views:

20

answers:

3

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

+1  A: 

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.

Jonathan Feinberg
http://docs.djangoproject.com/en/dev/ref/forms/fields/#datetimefieldthen what is this all about
seena
That's all about, as I said, "when you represent the date as a string, or when you wish to interpret a string as a date".
Jonathan Feinberg
+1  A: 

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')
ayaz
can u please tell me how can i use it ...... because its no where given
seena
You use it in your settings.py file (for example DATETIME_FORMAT = 'N j, Y, P'). You can find allowed options here: http://docs.djangoproject.com/en/dev/ref/templates/builtins/#ttag-now
3lectrologos
A: 

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
jathanism