user606521
user606521

Reputation: 15474

django time zone for printing date

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" ?

Upvotes: 3

Views: 7381

Answers (2)

catherine
catherine

Reputation: 22808

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

{% now h:ia %}

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

Upvotes: 0

eLRuLL
eLRuLL

Reputation: 18799

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.

Upvotes: 2

Related Questions