Django : Convert UTC to local time zone in 'Views'
I created this little function to solve the problem in a project:
import pytz
from django.utils import timezone
def convert_to_localtime(utctime):
fmt = '%d/%m/%Y %H:%M'
utc = utctime.replace(tzinfo=pytz.UTC)
localtz = utc.astimezone(timezone.get_current_timezone())
return localtz.strftime(fmt)
An used like:
utcdate = convert_to_localtime(date_from_db)
I also installed this app: django-tz-detect
from django.utils import timezone
local_dt = timezone.localtime(date_from_db) if date_from_db is not None else None