We want to get the current time in India in our Django project. In settings.py, UTC is there by default. How do we change it to IST?
In general, Universal Time(UTC) based on the mean sidereal time as measured in Greenwich, England. It's also approximately equal to mean solar time from Greenwich. If Daylight Saving Time is in effect in the time zone, one must add 1 hour to the above standard times.
Django got all these timezone support, The django.utils timezone module, just returns datetime based on the USE TZ
setting, they are simply datetime objects with timezone 'awareness'.
For IST(India Standard Time), which is UTC + 5:30, set TIME_ZONE = 'Asia/Kolkata'
and USE_TZ = True
, also USE_I18N = True, USE_L10N = True
, which are Internationalization and localization settings.
For a reference,
from django.utils import timezone
import datetime
print(timezone.now()) # The UTC time
print(timezone.localtime()) # timezone specified time,if timezone is UTC, it is same as above
print(datetime.datetime.now()) # default localmachine time
# output
2020-12-11 09:13:32.430605+00:00
2020-12-11 14:43:32.430605+05:30 # IST is UTC+5:30
2020-12-11 14:43:32.510659
refer timezone settings in django docs for more details.