3

I have django project with TIME_ZONE = 'America/Chicago' set in settings.py.

I am using dates like this:

from django.utils.dateparse import parse_datetime
import datetime
from datetime import datetime

dateobject = parse_datetime(some_string)
now = datetime.now()
if now > dateobject:
    # do something

Then in template I use:

dateobject|date:"d M Y H:i"

All I want to display dates in ALL my templates in different time zone (for example +6 hours). But I want project timezone to be 'America/Chicago' and date returned by datetime.now() was in this timezone. So I just want to change all dates DISPLAYED to be +6 hours for example. How I can accomplish this?


edit

Okay this works for me:

{% load tz %}
{{ datetimevalue|timezone:"Europe/Paris" }}

It prints date with +1 hour. But this doesn't work:

{% load tz %}
{% timezone "Europe/Paris" %}
{{ datetimevalue }}
{% endtimezone %}

It prints date unchanged. I have to set it globally for all templates. How I can do it?

I always use date filter (dateobject|date:"d M Y H:i") so maybe is there any way to "hook" this filter globally to change it to dateobject|timezone:"Europe/Paris"|date:"d M Y H:i" ?

user606521
  • 14,486
  • 30
  • 113
  • 204

2 Answers2

2

This is more a python question, you can use the timedelta variable to add time to a datetime. for example

import datetime
b = datetime.datetime.now() + datetime.timedelta(hours=n)

Or you can directly change the timezone of the datetime, for example:

datetime.astimezone(GMT2())

Or with the tzinfo variable in the now functions:

datetime.datetime.now(EST()) 

So these changes have to be done before you pass something to your templates.

eLRuLL
  • 18,488
  • 9
  • 73
  • 99
  • I have to set it globally for all my django project - I cant track every date function use and modify it... – user606521 Jan 31 '13 at 13:44
  • Then, you should reconfigure your app, but you also can do it from the template with these features https://docs.djangoproject.com/en/dev/topics/i18n/timezones/ – eLRuLL Jan 31 '13 at 13:59
  • I edited my question - your link helped me but not completely – user606521 Jan 31 '13 at 20:32
  • You can use `template inheritance`. If this is helping, you should show appreciation. I am also looking that my answer was what you were looking for, but know you have *changed* what you want. – eLRuLL Jan 31 '13 at 20:55
  • I wrote that timezone tags doesnt work - only timezone filter - so I cant include this in template inheritance... – user606521 Jan 31 '13 at 21:08
  • I posted new question to be more precise what I am looking for: http://stackoverflow.com/questions/14635446/pytz-timezone-tags-to-adjust-date-printed-in-templates – user606521 Jan 31 '13 at 21:27
0

See: http://docs.djangoproject.com/en/dev/ref/settings/#time-zone.

{% now h:ia %}

Try this one ..........

catherine
  • 22,492
  • 12
  • 61
  • 85