DEV Community

Cover image for Handle Timezone with Django
Mangabo Kolawole
Mangabo Kolawole Subscriber

Posted on

Handle Timezone with Django

A truth for people all around the world is that we experience time differently.

Trying to have the same time in your application for all users will not reflect real-time in their zone and mostly trying to register their different times in your database will result in more conflicts and errors.

For example, if you are looking to query all users who registered in your application for a certain period, this will be quite impossible because all entries are stored in their respective time zone.

To handle this, the world uses the Coordinated Universal Time (UTC). A time zone refers to any region where the same standard time is kept. For example, I am in UTC + 1 timezone and the time I am writing this article is 06:36 AM.
In India for example, where the timezone is UTC + 5, the time will be 11:36 AM. Interesting!🤩

Timezone in Django

In order to make sure that your application deal with UTC regardless of the location of the user, make sure to have the following configurations in your settings.py.

TIME_ZONE = 'UTC'
USE_TZ = True
Enter fullscreen mode Exit fullscreen mode

Then anywhere you are using datetime.now() in your application, this will always create time in UTC.

Top comments (0)