Reputation: 2238
When I use the following
created = models.DateTimeField('date published')
it displays time like this
23:12:06
instead of
11:12:06
I want it to display time in 12 hr not 24. Also when I press the now widget the time is 4 minutes behind. Any help would be appreciatecd
Upvotes: 4
Views: 10478
Reputation: 106
Django have built-in date formatting, like this:
created = DateTimeField(input_formats=['%Y-%m-%d %I:%M %p'])
Here is the full cheat-sheet about how this letters work:
http://blog.tkbe.org/archive/date-filter-cheat-sheet/
So, to implement 12hour format, use 'h' or 'g' depending on if you want to have leading zeros or not
It is also mentioned in django doc: https://docs.djangoproject.com/en/dev/ref/forms/fields/#datetimefield
This is for dev version, i dont know which one you are using
time is 4 minutes behind
Is your TIME_ZONE in settings correct ?
Upvotes: 1
Reputation: 5561
You can change input format for time field as follow:
created = TimeField(widget=TimeInput(format='%I:%M:%S'))
or
created = TimeField(input_formats=('%I:%M:%S'))
The %I indicates a 12 hour clock format whereas the %H indicates a 24 hour clock format.
in django we have some defined formats
time
'%H:%M:%S', # '14:30:59'
'%H:%M:%S.%f', # '14:30:59.000200'
'%H:%M', # '14:30'
date
'%Y-%m-%d', '%m/%d/%Y', '%m/%d/%y', # '2006-10-25', '10/25/2006', '10/25/06'
'%b %d %Y', '%b %d, %Y', # 'Oct 25 2006', 'Oct 25, 2006'
'%d %b %Y', '%d %b, %Y', # '25 Oct 2006', '25 Oct, 2006'
'%B %d %Y', '%B %d, %Y', # 'October 25 2006', 'October 25, 2006'
'%d %B %Y', '%d %B, %Y', # '25 October 2006', '25 October, 2006'
datetime
'%Y-%m-%d %H:%M:%S', # '2006-10-25 14:30:59'
'%Y-%m-%d %H:%M:%S.%f', # '2006-10-25 14:30:59.000200'
'%Y-%m-%d %H:%M', # '2006-10-25 14:30'
'%Y-%m-%d', # '2006-10-25'
'%m/%d/%Y %H:%M:%S', # '10/25/2006 14:30:59'
'%m/%d/%Y %H:%M:%S.%f', # '10/25/2006 14:30:59.000200'
'%m/%d/%Y %H:%M', # '10/25/2006 14:30'
'%m/%d/%Y', # '10/25/2006'
'%m/%d/%y %H:%M:%S', # '10/25/06 14:30:59'
'%m/%d/%y %H:%M:%S.%f', # '10/25/06 14:30:59.000200'
'%m/%d/%y %H:%M', # '10/25/06 14:30'
'%m/%d/%y', # '10/25/06'
you can use these formats to customize your fields
also see official doc
Upvotes: 3