Kolawole Mangabo

Handle Timezone with Django.

A truth for people worldwide 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, suppose you are looking to query all users who registered in your application for a certain period. In that case, this will be impossible because all entries are stored in their respective time zone.

The world uses the Coordinated Universal Time (UTC) to handle this. 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

To make sure that your application deals with UTC regardless of the user’s location, make sure to have the following configurations in your settings.py.

TIME_ZONE = 'UTC'
USE_TZ = True

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

Article posted using bloggu.io. Try it for free.