Is there a way to change default forms.TimeInput widget to 24h format isntead of 12 am, pm one?
from django import forms
class DatePickerInput(forms.DateInput):
input_type = 'date'
class TimePickerInput(forms.TimeInput):
input_type = 'time'
Pass a format. By default it respects your locale which is probably US something but you can override.
If you are generating an <input type="date" ..>
tag, then that formatting is actually controlled by the browser - you don’t have any control over it from the server. (It’s why Django, by default, uses type="text"
)
1 Like
I’m using django time widget only in html file im passing this form
from .widgets import DatePickerInput, TimePickerInput
from django.forms import ModelForm
from .models import Task
class CreateForm(ModelForm):
class Meta:
model = Task
fields = ['title', 'start_date', 'start_date_time', 'end_date','end_date_time', 'complete']
widgets = {
'start_date' : DatePickerInput(),
'start_date_time' : TimePickerInput(),
'end_date' : DatePickerInput(),
'end_date_time' : TimePickerInput(),
}
TimeInput or DateInput - no real difference as far as this topic is concerned.
Django uses the input_type="text"
for these precisely to be able to format the input and output. When you use input_type="time"
or input_type="date"
, the browser is going to control the formatting.
See <input type="time"> - HTML: HyperText Markup Language | MDN