user3599803
user3599803

Reputation: 6994

Django change local datetime format

I have a Django app in which I use the translation system. I want Django to print datetime objects in local format, but not in the default format in currently print for me.

Example, for english translation, I get the datetime objects in this format: May 14, 2015, 1:26 p.m. And I want to get this format: mm/dd/yyyy hh:mm i.e 03/14/2015 13:26

For other langauges I still get the month name in the output in the local language (like 'May 14'), and I dont want that. For other languages I just want dd/mm/yyyy hh:mm

Upvotes: 0

Views: 963

Answers (1)

Julien Grégoire
Julien Grégoire

Reputation: 17124

You can set custom format for different languages.

In settings:

FORMAT_MODULE_PATH = [
    'mysite.formats',
    'some_app.formats',
]

Then create the files like this:

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

See complete ref here: https://docs.djangoproject.com/en/1.8/topics/i18n/formatting/#creating-custom-format-files

Then in formats.py, you set it as you want. For english:

import datetime    

DATETIME_FORMAT = 'm/d/Y h:i'

Ref: https://docs.djangoproject.com/en/1.8/ref/templates/builtins/#std:templatefilter-date

Upvotes: 2

Related Questions