Working with Datetime Objects and Timezones in Python
Last Updated :
18 Jun, 2025
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/New_York or add 90 days to the current time using a timedelta.
Let’s go through the most useful operations when working with datetime and timezones.
Getting a Datetime object
1. Using now() method
A very easy way to get a Datetime object is to use the datetime.now() method. A DateTime object is an instance/object of the datetime.datetime class. The now() method returns an object that represents the current date and time.
Python
import datetime
# getting the datetime object of current date and time
print(datetime.datetime.now())
Output2025-06-17 12:32:58.612718
Explanation:
- datetime.datetime.now() returns the current date and time with microseconds.
- The output includes year, month, day, hour, minute, second, and microsecond.
2. Defining the Datetime object manually
We can also declare the DateTime object manually
Python
import datetime
# initialising the datetime object
obj = datetime.datetime(2001, 12, 9)
print(obj)
Output2001-12-09 00:00:00
Explanation:
- You manually create a datetime for 9th December 2001.
- Time defaults to midnight if not specified.
After this, we learn how to format our Datetime objects.
For printing our datetime objects in different formats, we use strftime() method. The syntax of strftime() is:
strftime(format)
Python
import datetime
obj = datetime.datetime(2001, 12, 9)
# Example 1
print(obj.strftime("%a %m %y"))
# Example 2
print(obj.strftime("%m-%d-%Y %T:%M%p"))
OutputSun 12 01
12-09-2001 00:00:00:00AM
Explanation:
- strftime() converts a datetime object into a formatted string.
- %a = short weekday name, %m = month, %y = two-digit year.
- %T = full time (HH:MM:SS), %p = AM/PM.
Extracting Hour, Minute, and Second
We can get different attributes of the datetime object. In the code below, we are going to get the hours, minutes, and seconds.
Python
import datetime
obj = datetime.datetime(2001, 12, 9, 12, 11, 23)
# Hours
print(obj.hour)
# Minutes
print(obj.minute)
# Seconds
print(obj.second)
Explanation: You can access time components directly using attributes like .hour, .minute, and .second.
Creating Datetime from String Using strptime()
We can also get a datetime object using the strptime() method, in which we get a DateTime object from a string. Since the strings we use can be formatted in any form, we need to specify the expected format. To understand this, let's look at the syntax of strptime():
Syntax:
datetime.strptime(data,expected_format)
Parameters :
- data : Our time and date passed as a string in a particular format
- expected_format : the format in which data is presented in the first parameter
Python
import datetime
dt = datetime.datetime.strptime("Jan 1, 2013 12:30 PM", "%b %d, %Y %I:%M %p")
print(dt)
Output2013-01-01 12:30:00
Explanation:
- strptime() parses the string according to the given format.
- %b = month abbreviation, %I = hour (12-hour), %p = AM/PM.
Working with Timezones
The DateTime objects that we have been working with till now are what are known as naive DateTime objects. A naive DateTime object does not have any information on the timezone. We can check this using the tzinfo property.
Python
import datetime
obj = datetime.datetime(2001, 11, 15, 1, 20, 25)
# checking timezone information
print(obj.tzinfo)
Explanation:
- The datetime object is naive and doesn't contain any timezone data.
- .tzinfo returns None for naive datetimes.
To set our own timezones, we have to start using the pytz module. In the next example, we will create a DateTime object first and then create a timezone object. We will then localize that timezone object to the DateTime object and check the tzinfo property.
Python
import datetime
import pytz
obj = datetime.datetime(2001, 11, 15, 1, 20, 25)
# defining the timezone
tz = pytz.timezone('Asia/Kolkata')
# localising the datetime object to the timezone
aware_obj = tz.localize(obj)
# checking timezone information
print(aware_obj.tzinfo)
Explanation:
- pytz.timezone() defines a timezone object.
- .localize() makes the naive datetime timezone-aware.
Conversion of DateTime object from one Timezone to another
To convert the DateTime object from one timezone to another we need to use the astimezone() method.
Syntax:
DateTimeObject.astimezone(tz=None)
where:
- tz : The specified timezone to which the DateTimeObject needs to be converted to
returns: a datetime instance according to the specified time zone parameter tz
Python
import datetime
import pytz
# defining the object and localising it to a timezone
dt = datetime.datetime(2001, 11, 15, 1, 20, 25)
tz = pytz.timezone('Asia/Kolkata')
dt = tz.localize(dt)
# Creating a new timezone
new_tz = pytz.timezone('America/New_York')
# Changing the timezone of our object
converted = dt.astimezone(new_tz)
# Printing out new time
print(converted)
Output2001-11-14 14:50:25-05:00
Explanation:
- astimezone() converts a datetime object to another timezone.
- The converted object reflects the time in America/New_York with offset.
Similar Reads
Python | Working with date and time using Pandas While working with data, encountering time series data is very usual. Pandas is a very useful tool while working with time series data. Pandas provide a different set of tools using which we can perform all the necessary tasks on date-time data. Let's try to understand with the examples discussed b
8 min read
How to make a timezone aware datetime object in Python 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
3 min read
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
Convert string to datetime in Python with timezone Converting a string to a datetime in Python with timezone means parsing a date-time string and creating a timezone-aware datetime object. For example, a string like '2021-09-01 15:27:05.004573 +0530' can be converted to a Python datetime object that accurately represents the date, time and timezone.
2 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
Convert Datetime Object To Local Time Zone In Pandas Pandas has established itself as one of the most powerful and versatile libraries in Python. When working with time series data, handling datetime objects efficiently becomes paramount. Oftentimes, datasets contain timestamps in various time zones, necessitating conversion to a consistent reference
2 min read