3

I am using django-model's-dateField in my django-project and wanna change my dateFields format in admin view.

Currently (default) django gives format in YYYY-MM-DD but i want to modify that and in need of DD-MM-YYYY

I've been struggled too much in this and till no solution.

Also providing my code, may help..

my settings.py have:

LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True

models.py:

class UserProfile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='User_Profile')
    mobile = models.BigIntegerField()
    alternative_number = models.BigIntegerField()
    DOB = models.DateField()

and then i register it in admin

output on admin

as you can see in my admin page there is a dateField with format 2019-11-04 but i want in form of DD-MM-YYYY as 04-11-2019.

I tried LANGUAGE_CODE = 'de-at' that would work for me but the problem is, it changes the local language also.

refering a similer post: Django - change default presentation format of model date field

Vijay
  • 93
  • 2
  • 9
  • What did you think of the links in the answers to this question: [how-to-change-django-datetime-format-output/11578330#11578330](https://stackoverflow.com/questions/11578311/how-to-change-django-datetime-format-output/11578330#11578330) – Tamoor Salah-U-Din Nov 19 '19 at 10:13
  • Hi @TamoorSalah-U-Din, these are for forms.datefield, i tried for mine but didn't solve the problem. – Vijay Nov 19 '19 at 13:10

3 Answers3

1

You need just to override Django’s defaults locale settings. Add this to your settings.py file

from django.conf.locale.es import formats as es_formats

es_formats.DATETIME_FORMAT = "d M Y H:i:s"
  • This is not working for my case can you please elaborate what the package django.conf.locals. **es** Is the *es* for LANGUAGE_CODE or what.. in my case i have LANGUAGE_CODE = "en-us" does this related with this. – Vijay Nov 19 '19 at 12:04
  • `es` will refer to Spain / Spanish. Replacing `es` with `en` may work, you might need to play around with it a little. – Harry Vane Nov 19 '19 at 13:29
1

fahim kazi was right: to define a models.DateField format, you need DATE_FORMAT, not DATETIME_FORMAT.

Yet Naveen Varshney was also right: not DATE_FORMAT nor DATETIME_FORMAT work without specifying the language, with es_formats for instance.

Therefore, from the combination of both answers, put the following in settings:

es_formats.DATE_FORMAT = 'd-m-y'

(Also '%d-%m-%y' printed %01-%08-%20 thus the % is not needed).

Thanks both.

Alvaro Rodriguez Scelza
  • 3,643
  • 2
  • 32
  • 47
0

add this to your settings.py

DATE_FORMAT = '%d-%m-%y'

for more check this date format

fahim kazi
  • 71
  • 5