views:

101

answers:

0

I'm building an Django app that requires users to enter the time of day in TimeFields and I'm trying to format this as nicely as possible. My code currently looks like this:

start = forms.TimeField(widget=forms.TimeInput(format="%I:%M %p"))
end = forms.TimeField(widget=forms.TimeInput(format="%I:%M %p"))

Where the values in the format strings come from http://docs.python.org/library/time.html#time.strftime. The problem with this is that once users have entered a time into the form, it's displayed back in a very ugly manner - with leading zeroes for the hours and a great big capitalized AM or PM (e.g., 09:30 AM as opposed to something like 9:30 a.m.)

On the other hand, the built-in 'time' template filter and (I believe) a few other parts of Django use this scheme for time formatting: http://docs.djangoproject.com/en/dev/ref/templates/builtins/#ttag-now which is much more flexible!

The question is this: What's the easiest way to have the TimeInput widgets display the time data in a more flexible way or, at least, in some prettier way. And also, if you happen to know, why the inconsistencies in Django's time/date formatting? Why not just stick to the latter method since it's more powerful?