I have a meeting model with a start time column:
class Meeting(mode.Model):
name = models.CharField(max_length=100)
start = models.DateTimeField()
My time zone is set to TIME_ZONE = 'America/Toronto'
I’m currently entering the EDT value of the time. In my template, I want to display both the EDT and PDT values, for example:
| Name | Start |
| First Meeting | 10:00 AM PDT |
| | 1:00 PM EDT |
Is it currently possible in Django to display two different timezone values from a single entry?
Depends how you want it to render. I’d make a templatetag, and then you could put things like
{{ start_time|pdt_edt }}. The benefit of this method over defining functions in the class objects is that you can reuse the filter for other datetimes that need to be printed in both formats. (E.g., if your meeting had an end time; or another object had a time that you’d like to list in both formats).I’d suggest starting with pytz to handle (install
easy_install.py pytz).Basically make a sub-directory of your app called
templatetags/put in an__init__.pyand another file call it something saytime_zone_display.pyfor your template tag. (Basic procedureIn your template include
And in
time_zone_display.pysomething likeYou could also simplify the template if you know that your settings.TIME_ZONE will always be a fixed offset (3 hours) from pacific time, so then you could just do something like:
(Though EDT/PDT are only the correct abbreviations during daylight’s savings time; so the first method is better thought it requires installing pytz).
EDIT: the rare case when Eastern time and pacific time are not off by three hours, but pytz will handle correctly.