0

I use UTC in my Django app and I want to display the time for my local timezone; 'Europe/Stockholm'. I just can't get this simple thing to work so I must be doing something wrong. I just can't figure out what it is...

This is what I have in my settings.py:

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

And this is how I get the date that is passed to my template:

my_time = datetime.datetime.utcnow()

And finally this is what i have in my template file:

{% load tz ‰}

{% timezone 'Europe/Stockholm' %}{{ my_time|time }}{% endtimezone %}

But whatever timezone I use in {% timezone ... %} the date always read the same 20:31, and the local time should in fact be 22:31.

Thankful for any help with this!

2 Answers2

2

in settings.py change TIME_ZONE = 'UTC' to TIME_ZONE = 'Europe/Stockholm'

You also need to replace utcnow() method with now()

my_time = datetime.datetime.now()
alv2017
  • 752
  • 5
  • 14
Mugoya Dihfahsih
  • 425
  • 3
  • 11
1

The normal way to use local times is to set TIME_ZONE or call activate().

That said, you can use the timezone template tag if you want to. The problem is with utcnow(). As noted in the documentation, that returns a naive datetime, which means that it's not timezone-aware. Use Django's now() instead; that will return a timezone-aware datetime that can be adjusted.

Kevin Christopher Henry
  • 46,175
  • 7
  • 116
  • 102