Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save brandenhall/931313f450e9d602e1f9fdcf360fe06d to your computer and use it in GitHub Desktop.
Save brandenhall/931313f450e9d602e1f9fdcf360fe06d to your computer and use it in GitHub Desktop.
Customizing the Date & Time Format in Django with USE_L10N enabled

With the USE_L10N setting enabled (which is the default in Django 4) you can't override date/time formatting in your settings.py - it has to be done at the locale level.

The default date/time formatting for English uses 24-hour time. But, if you're in the US you commonly need to change it use 12-hour time. This is how you do that without disabling USE_L10N.

See https://docs.djangoproject.com/en/4.1/ref/settings/#format-module-path for more details.

Within your site's main folder (where your settings.py lives), create this structure:

formats /
  __init__.py
  en /
    __init__.py
    formats.py

Then add the line below to your settings.py (assuming your site is named mysite) and finally, populate the formats.py with the formatting you want to use.

TIME_INPUT_FORMATS = ('%I:%M %p',)
TIME_FORMAT = 'g:i A'
DATETIME_INPUT_FORMATS = ['%Y-%m-%d %I:%M %p',]
DATETIME_FORMAT = 'M j, Y @ g:i A'
FORMAT_MODULE_PATH = ['mysite.formats',]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment