Epicserve

Change the DateField format in the Django Admin

July 15, 2011 | 4:17 p.m. CDT

Recently I was working on a Django project where I decided I wanted a different format other than the standard YYYY-MM-DD. I searched the online documentation and Google and it took a little longer than expected to figure out how to make the format change, so I thought I’d make a blog post to help the next guy or girl that needs to figure it out.

Why the default format for English is YYYY-MM-DD, I’ll never know. Everyone I know in the US uses MM-DD-YYYY.

The default Django 1.3 settings file comes with USE_L10N set to True which means that if you try to override a setting like DATE_INPUT_FORMATS in your settings file it doesn’t work. So in order to change the default date and time formats you need to use the setting, FORMAT_MODULE_PATH in your settings file to give Django the path to where you want to put your custom locale formats.

So if you add the following to your settings file like I did:


FORMAT_MODULE_PATH = 'config.locale'

...then you would create the following directories and files in your project, if your LANGUAGE_CODE is set to en-us.


mysite/
    config/
        __init__.py
        locale
            __init__.py
            en/
                __init__.py
                formats.py

Next you would copy the default settings for the formats.py file and add them to the new formats.py file that you just created.

In order to change the date and time formats in the Django admin for the DateField and the DateTimeField you need to change the DATE_INPUT_FORMATS and make the first index in the DATETIME_INPUT_FORMATS tuple so it is the format that you want. I’ve updated my django-base-site repository on github.com if you want to look at how I set my project up.

I hope this helps!

Related tags: Django

Comments

SDC
1.   At 3:08 p.m. CDT on June 14, 2012, SDC wrote:

I've given this a try, however it always goes for the default Django format.py, FORMAT_MODULE_PATH has no apparent effect (Django 1.3.1).

Brent O'Connor
2.   At 3:14 p.m. CDT on June 14, 2012, Brent O'Connor wrote:

SDC, my guess is that you don't have FORMAT_MODULE_PATH set correctly. Where did you put your locale directory, what did you set FORMAT_MODULE_PATH to and what is DJANGO_SETTINGS_MODULE set to?

Derek
3.   At 3:12 p.m. CDT on July 8, 2012, Derek wrote:

"Why the default format for English is YYYY-MM-DD, I’ll never know. Everyone I know in the US uses MM-DD-YYYY."

The issue is that the US, unfortunately, does not follow the logic of the rest of the world (in the same way that their units system is still archaic). The logic of biggest->smallest is the reason for a year-month-day-hour-minutes-second format. It is not in any way logical to swop the month and day around... Sorry.

Brent O'Connor
4.   At 10:17 a.m. CDT on July 9, 2012, Brent O'Connor wrote:

It's logical if you look at a Calendar, the month is at the top and then the days are listed. Anyway, it shouldn't matter if it's not logical to the rest of the world because it's the standard that the US uses.

Russ
5.   At 1:18 p.m. CST on Jan. 10, 2013, Russ wrote:

@Brent On most calendars, before you get to a page with a month on it, you'll find a pretty one with a big fat YYYY on it, which would actually support the rest-of-the world's position.

The 'standard that the US uses' is so standard that the US military uses various conflicting standards. This document provides a hint, but this is by no means the end of the story: http://www.armystudyguide.com/content/Prep_For_Basic_Trai...

For all the important military cases, dates seem to be either y.m.d or d.m.y (where d,m,y are my very loose definitions of the obvious units, and where details vary by use). Any serious use that puts d in the middle seems to be asking for ridicule and confusion.

Brent O'Connor
6.   At 1:49 p.m. CST on Jan. 10, 2013, Brent O'Connor wrote:

@Russ,

It depends on the use case. If I was making a websites where the admins were in the Military or from Europe I would adjust the date and time format for the admin accordingly.

The point is, you can adjust this for the people that are using the admin site to make it more friendly for them.

Post Your Comment

(Optional & Not Displayed)

(Optional)


(allowed tags - strong, em, cite, & code)

Post Guidelines

Please be considerate of others. Keep comments relevant. Content deemed inappropriate or offensive may be edited and/or deleted. Email addresses are never displayed.

Line breaks and paragraphs are automatically converted — no need to use p or br/. Quotes, apostrophes, and double-dashes are automatically converted to smart punctuation. Be careful when copying and pasting portions of entries or other comments.

Links can be created using the standard <a href="http://url">urlName</a>. The following inline HTML elements may also be used: strong, em, cite, & code. The title attribute is allowed within any element. All other code will get removed before posting.