ratata
ratata

Reputation: 1169

Django template tag, python timezone aware dates different results

I have :

TIME_ZONE = 'Europe/Paris'
USE_L10N = True
USE_TZ = True

in my settings.py file. I am living in Istanbul/Turkey and there is one hour difference between Paris and Istanbul.

In the admin side when selecting a date, django correctly shows 1 hour difference. And using template tag i am getting the datetime i have set in the admin.

But when i pass the datetime via python using beginning_date.strftime("%H:%M") python substracts 1 hour from the value that was set via admin which is not true.

How can i solve this?

Upvotes: 1

Views: 586

Answers (2)

allcaps
allcaps

Reputation: 11228

Use the Django template defaultfilters to format your dates in Python code.

from django.template.defaultfilters import date as _date

_date(datetime_object, "%H:%M")

And, maybe related: Django cannot reliably use alternate time zones in a Windows. See documentation.

Upvotes: 2

Kevin Christopher Henry
Kevin Christopher Henry

Reputation: 48902

I don't think Turkey has anything to do with it.

My guess is that the one-hour difference you're seeing is between the Paris timezone—which is being used, by default, to interpret and display dates—and UTC—which is being used to store the datetime, and which is the timezone of the datetime returned from the database.

If that's correct, then you can just use django.utils.timezone.localtime to convert the datetime to the current time zone (which by default will be TIME_ZONE):

localtime(beginning_date).strftime("%H:%M")

Upvotes: 0

Related Questions