Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django datetimefield time in template

I have a DateTimeField in one of my models

If I display {{ model.datetime }} in the template, I see:

Aug. 13, 2013, 7:57 p.m.

If I display {{ model.datetime.time }} in the template , I see:

2:57 am

I want it to just display 7:57 p.m. How can I get it to use the correct timezone. I have tried with model.datetime.timetz as well but with the same result.

like image 654
bwbrowning Avatar asked Aug 15 '13 04:08

bwbrowning


People also ask

How to get current datetime in Django template?

now tag displays the current date and/or time, using a format according to the given string. Such string can contain format specifiers characters as described in the date filter section.

How do I get UTC time in Django?

The solution to this problem is to use UTC in the code and use local time only when interacting with end users. Time zone support is disabled by default. To enable it, set USE_TZ = True in your settings file. In Django 5.0, time zone support will be enabled by default.

What is the format of Django DateTimeField?

DATETIME_FORMAT="%Y-%m-%d%H:%M:%S"


1 Answers

Try

{{ model.datetime|date:"g:i a" }}

If you want a different format, refer to the documentation here

like image 191
karthikr Avatar answered Sep 22 '22 01:09

karthikr