[Django]-Django how to manage format time with only hour and minute

4๐Ÿ‘

You need to add a single line to the settings.py file and it will change the time formatting across all the apps in your project.

add this to the settings.py file:

  • If you want the 12 Hr clock formatting with AM/PM: TIME_INPUT_FORMATS = ('%I:%M %p',)
  • If you want 24 Hour clock formatting: TIME_INPUT_FORMATS = ('%H:%M',)

1๐Ÿ‘

in setting.py add

'DATETIME_FORMAT': "%m-%d - %M:%S"

in models.py

temps_preparation = models.DateTimeField(null=True)

and in form.py

datetime.strptime(yourdate, "%M:%S")

๐Ÿ‘คMaede

0๐Ÿ‘

You can format dates directly in the html template (docs)

<p>{{ your_date|date:"h:iA" }}</p>

or in the view (docs)

your_date.strftime("%I:%M%p")

In the example above I used the 12-hours format.

๐Ÿ‘คDos

0๐Ÿ‘

Using TimeField and setting seconds to 00 before

See: TimeField Model in Doc

๐Ÿ‘คc24b

Leave a comment