How to make a timezone aware datetime object in Python
Last Updated :
27 May, 2025
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)
Output2025-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)
Output2025-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)
Output2025-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)
Output2025-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.
Related articles
Similar Reads
How to add time onto a DateTime object in Python In Python, adding time (such as hours, minutes, seconds, or days) to a datetime object is commonly done using the timedelta class from the datetime module. This allows precise manipulation of date and time values, including performing arithmetic operations on datetime objects. It's key features incl
3 min read
How to remove timezone information from DateTime object in Python In Python, the tzinfo attribute of a datetime object indicates the timezone information. If tzinfo is None, the object is naive i.e., has no timezone. Otherwise, it's aware. Example:Pythonfrom datetime import datetime, timezone dt_aware = datetime.now(timezone.utc) print("With tzinfo:", dt_aware) pr
3 min read
Working with Datetime Objects and Timezones in Python Python provides tools for managing date and time using the datetime module. Whether you're fetching the current time, formatting it, handling timezones or calculating time differences, this module makes it simple. For example, you can easily convert "2001-11-15 01:20:25" from Asia/Kolkata to America
4 min read
How to convert Python's .isoformat() string back into datetime object In this article, we will learn how to convert Pythons .isoFormat() string back into a datetime object. Here we are using the current time and for that, we will store the current time in the current_time variable. The function now() of this module, does the job perfectly. Example: Getting current tim
1 min read
How to convert DateTime to integer in Python Python provides a module called DateTime to perform all the operations related to date and time. It has a rich set of functions used to perform almost all the operations that deal with time. It needs to be imported first to use the functions and it comes along with python, so no need to install it s
2 min read