Hello how do i change the date format in models for Eg: i have date = models.DateField(blank=True, null=True)
by default it yyyy-mm-dd format how do i change it to dd-mm-yyyy format
Hello how do i change the date format in models for Eg: i have date = models.DateField(blank=True, null=True)
by default it yyyy-mm-dd format how do i change it to dd-mm-yyyy format
A date is a date, and doesn't have a "format". A format applies when you represent the date as a string, or when you wish to interpret a string as a date.
You could play around with DATE_INPUT_FORMATS and similar settings for your project.
You define the setting in the settings.py
file for your project. For instance:
DATE_INPUT_FORMATS = ('%d-%m-%Y', '%Y-%m-%d', '%m/%d/%Y', '%m/%d/%y', '%b %d %Y',
'%b %d, %Y', '%d %b %Y', '%d %b, %Y', '%B %d %Y',
'%B %d, %Y', '%d %B %Y', '%d %B, %Y')
You're better off worrying about the format on output, not how it is stored. A DateField
is stored as your database's native column of the same type.
If you really want this, you can set DATE_FORMAT
in your settings.py
, like so:
DATE_FORMAT='d-m-Y'
This makes a default display like this:
Feb 5, 2010
Look like this:
05-02-2010
Now of course there are other places where this will be overridden, such as the admin app and default form widgets, so it's not like this is a total solution to all your problems. Just get used to using datetime.datetime.strftime()
like so:
>>> # using one of my own apps as an example where 'created' is a DateTimeField
>>> r.created
datetime.datetime(2010, 1, 28, 10, 5, 19, 38963)
>>> print r.created # default output (this is a Python default)
2010-01-28 10:05:19.038963
>>> print datetime.datetime.strftime(r.created, "%m-%d-%Y") # forcing it to m-d-Y format
01-28-2010