Search code examples
djangodjango-viewstimezonedjango-timezone

How to show UTC time in local timezone in Django view?


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!


Solution

  • 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()