Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
149 views
in Technique[技术] by (71.8m points)

How to display all datetime objects in a different timezone in Django

I am relatively new to Django.

All of my datetime objects are in UTC and my settings.py file has TIME_ZONE = 'UTC'

When these datetime objects show up in my html template using {{ obj.datetime }}, they are dislayed in UTC, I want to display them in UTC-05:00.

How can I change this?

Note: I am using class-based views


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can change timezone global in your settings.py set:

TIME_ZONE = 'America/Panama'

for UTC-05:00

Or you can use filter timezone in template - see Django documentation

{% load tz %}

{{ obj.datetime|timezone:"America/Atikokan" }}

{% timezone "America/Panama" %}
    Panama time: {{ value }}
{% endtimezone %}

How can you see all available time zones?

pytz provides helpers, including a list of current time zones and a list of all available time zones – some of which are only of historical interest.

List of timezones from wikipedia: https://en.wikipedia.org/wiki/List_of_tz_database_time_zones


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
...