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
Timezone is defined as a geographical area or region throughout which standard time is observed. It basically refers to the local time of a region or country. Most of the time zones are offset from Coordinated Universal Time (UTC), the worldâs standard for time zone. In this article, we will discuss
2 min read
Working with Datetime Objects and Timezones in Python
In this article, we are going to work with Datetime objects and learn about their behavior when Time zones are introduced. We are going to be working with the Python datetime module. Getting a Datetime objectMethod 1: Using now() method A very easy way to get a Datetime object is to use the datetime
5 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
Add Months to datetime Object in Python
In this article, let's delve into the techniques for Add Months to datetime Object in Python. Working with dates and times often requires manipulation and adjustment, and understanding how to add months to a datetime object is a crucial skill. We will explore various methods and libraries to achieve
3 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
How to convert datetime to date in Python
In this article, we are going to see how to convert DateTime to date in Python. For this, we will use the strptime() method and Pandas module. This method is used to create a DateTime object from a string. Then we will extract the date from the DateTime object using the date() function and dt.date f
3 min read
Add Years to datetime Object in Python
In this article let's learn how to add years to a datetime object in python. Python Datetime module supplies classes datetime. These classes provide us with a number of functions to deal with dates, times, and time intervals. Date and datetime are an object in Python, so when you manipulate them, y
2 min read
Manipulate Date and Time with the Datetime Module in Python
Have you ever wondered about working with Date and Time with Python? If you have then you must have noticed that Python does not provide any built-in method to work with either Date or Time. But thanks to the DateTime module that comes pre-loaded with Python's standard utility modules we can easily
9 min read
How to create filename containing date or time in Python
Prerequisite: DateTime module In this article, we are going to see how to create filenames with dates or times using Python. For this, we will use the DateTime module. First, import the module and then get the current time with datetime.now() object. Now convert it into a string and then create a f
2 min read