Search code examples
djangodatetimeformatpython-datetimedjango-3.2

Change DateTimeField FORMAT in Django version 3.2.2


I'm trying to change DateTime format for all date time objects in my project. I want to format :

    21-06-2021 14:02:12

My settings

DATETIME_FORMAT = '%d-%m-%Y %H:%M:%S'
TIME_ZONE = 'Africa/Tunis'
USE_I18N = True
USE_L10N = False  
USE_TZ = True 

Result:

%21-%06-%2021 %14:%Jun:%st

Solution

  • The DATETIME_FORMAT setting [Django-doc] works with the formatting specifications like PHP does that.

    So as format, you should use:

    DATETIME_FORMAT = 'd-m-Y H:i:s'

    Note that the formatting is slightly different [PHP datetime specs]. PHP uses i for the minutes; and a lowercase s for the seconds:

    format character Description Example
    i Minutes with leading zeros 00 to 59
    s Seconds with leading zeros 00 through 59

    The Django documentation also has the format characters listed as @AbdulAzizBarkat says.