Django datetime field - convert to timezone in view
There's no need to use django.utils
to convert between timezones :
berlin = pytz.timezone('Europe/Berlin')
local_dt = item.created_at.astimezone(berlin)
Yet if you usually work with just one timezone it is convenient to store it in settings.TIME_ZONE = 'Europe/Berlin'
and then
local_dt = timezone.localtime(item.created_at)
will convert it to your localtime.
start with this:
from django.utils import timezone
local_dt = timezone.localtime(item.created_at, pytz.timezone('Europe/Berlin'))
To convert to UTC+1:
from django.utils import timezone
local_dt = timezone.localtime(item.created_at, timezone.get_fixed_timezone(60))
- How to get local time in current timezone (2nd question)
- localtime()
- get_fixed_timezone()