Open In App

How to make a timezone aware datetime object in Python

Last Updated : 27 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

A naive datetime in Python contains only the date and time, while a timezone-aware datetime includes timezone details, such as the UTC offset or timezone name, allowing it to accurately represent an exact moment in time. Creating a timezone-aware datetime means adding this timezone information to a naive datetime, making it clear which time and zone it belongs to. Let’s explore different efficient methods to achieve this.

Using datetime.timezone()

datetime.timezone() method lets you apply a fixed UTC offset (like +05:30) to a datetime object using timedelta. It’s simple and works well when you just need a static offset without worrying about time zone names or daylight saving time.

Python
from datetime import datetime, timezone, timedelta
a = timezone(timedelta(hours=5, minutes=30))  # +05:30 offset

b = datetime.now(a) # current time with offset
print(b)

Output
2025-05-24 12:17:08.756063+05:30

Explanation: This code creates a +05:30 timezone using timezone(timedelta(...)) for IST, then gets the current time in that timezone using datetime.now(), returning a timezone-aware datetime.

Using zoninfo.ZoneInfo()

zoneinfo.ZoneInfo() method allows the use of real-world timezone names like "Asia/Kolkata". It automatically handles daylight saving changes and is part of the standard library, making it ideal for accurate and dynamic timezone support.

Python
from datetime import datetime
from zoneinfo import ZoneInfo

a = datetime.now(ZoneInfo("Asia/Kolkata"))
print(a)

Output
2025-05-24 12:01:53.719893+05:30

Explanation: ZoneInfo("Asia/Kolkata") get the real timezone for IST, then datetime.now() returns the current time with correct daylight saving and timezone info as a timezone-aware datetime.

Using pytz timezone

pytz module is a widely-used third-party library for timezone conversions using region names. It handles daylight saving time and historical timezone data, making it reliable for legacy code and comprehensive timezone support.

Python
from datetime import datetime
import pytz

a = pytz.timezone("Asia/Kolkata")
b = datetime.now(a)
print(b)

Output
2025-05-24 12:03:38.489802+05:30

Explanation: pytz.timezone("Asia/Kolkata") gets the IST timezone, then datetime.now(a) fetches the current time in that timezone, returning a timezone-aware datetime with proper daylight saving handling.

Using replace(tzinfo=timezone(...))

replace(tzinfo=...) directly sets a timezone offset on a naive datetime without changing the time itself. It's useful for tagging datetimes with known offsets but doesn’t perform any actual conversion or validation.

Python
from datetime import datetime, timezone, timedelta

a = datetime(2025, 5, 24, 15, 0, 0)
a = a.replace(tzinfo=timezone(timedelta(hours=5, minutes=30)))
print(a)

Output
2025-05-24 15:00:00+05:30

Explanation: A naive datetime for May 24, 2025, 15:00 is created. Then replace(tzinfo=timezone(timedelta(hours=5, minutes=30))) attaches a fixed +05:30 offset (IST) to it without changing the time, making it timezone-aware.

Using pendulum library

pendulum library is a modern alternative to Python’s datetime module. It offers easy-to-use timezone support with clear syntax, automatically applying the correct local time when given a timezone like 'Asia/Kolkata'.

Python
import pendulum
a = pendulum.now('Asia/Kolkata')
print(a)

Output

2025-05-24 12:07:05.982390+05:30

Explanation: pendulum.now('Asia/Kolkata') returns the current Asia/Kolkata time with automatic timezone and daylight saving handling, using simple, clear syntax.


Next Article
Article Tags :
Practice Tags :

Similar Reads