Reputation:
How can i convert django datetime field to the format that shows in the template. Date time field in database is '2015-05-1 00:01:00', i want to show it in 'May 1, 2015, 12:01 a.m.' format in the view functions.
Upvotes: 1
Views: 2454
Reputation: 2103
That's not the django way, you should use django template formatters, this is how it works:
{{ value|date:"N j, o," }} {{ value|time:"P"}}
April 22, 2015, 11:53 a.m.
Where value is the datetime value you retrieve from the database with DateTimeField
You should make the changes in your templates because they are the one responsible for the display, your views shouldn't be bloated with something like the accepted answer
Edit: unless you are doing ajax lol
the docs: https://docs.djangoproject.com/en/dev/ref/templates/builtins/#date
Upvotes: 2
Reputation: 3827
import datetime
template_type_value = datetime.datetime.strptime(date, '%Y-%m-%d %H:%M').strftime('%b %d, %Y, %I:%M %p')
Upvotes: 0