[Fixed]-TimeField format in Django template

29👍

I had exactly this problem in my app, and solved it by passing a format parameter to Django’s TimeInput widget:

from django import forms

class SettingsForm(forms.Form):
    delivery_time = forms.TimeField(widget=forms.TimeInput(format='%H:%M'))

12👍

Assuming you have a format you want all TimeFields to use, you can add the TIME_INPUT_FORMATS to your settings file. For example, TIME_INPUT_FORMATS = ('%I:%M %p',) will format your times as “5:30 PM”.

The docs: https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-TIME_INPUT_FORMATS

👤Josh

Leave a comment