Create timezone aware datetime object in Python

In this article, we will discuss how to create a timezone-aware datetime object in Python. It means the tzinfo attribute in the datetime object should not be empty and contain the specified timezone info.

In Python’s datetime module, the datetime class has an attribute tzinfo, which contains the timezone information. If you create a naive datetime object i.e. a datetime object without timezone information, then the tzinfo in the datetime object will be null. There are two techniques to create a datetime object with timezone information i.e.

  1. By passing the tzinfo i.e. timezone information in the datetime contsructor while creating datetime object.
  2. By creating a naive datetime i.e. without timezone information and then assigning the timezone later.

Let’s see examples of both the techniques one by one.

Create timezone aware datetime object by passing tzinfo in the constructor

Create datetime object with UTC timezone

To create a datetime object with UTC timezone, pass the pytz.UTC as tzinfo argument in the datetime constructor. For example,

from datetime import datetime
import pytz

# Create datetime object with UTC timezone
dt_obj = datetime(  2021,   # Year
                    10,     # Month
                    4,      # Day
                    9,      # Hours
                    10,     # Minutes
                    34,     # Seconds
                    300030, # Microseconds
                    tzinfo=pytz.UTC )

print(dt_obj)

print(dt_obj.tzinfo)

Output:

Advertisements
2021-10-04 09:10:34.300030+00:00
UTC

It created a datetime object with UTC timezone. We confirmed it by checking that tzinfo attribute of the datetime object is not null.

Create datetime object with US/Alaska timezone

To create a datetime object with US/Alaska timezone, call the localize() function on pytz.timezone(‘US/Alaska’) and pass the datetime object as argument. For example,

from datetime import datetime
import pytz

# Create datetime object with US/Eastern timezone
dt_obj = datetime(  2021,   # Year
                    10,     # Month
                    4,      # Day
                    9,      # Hours
                    10,     # Minutes
                    34,     # Seconds
                    300030, # Microseconds
                )
dt_obj = pytz.timezone('US/Alaska').localize(dt_obj)

print(dt_obj)
print(dt_obj.tzinfo)

Output:

2021-10-04 09:10:34.300030-08:00
US/Alaska

It created a datetime object with ‘US/Alaska‘ timezone. We confirmed it by checking that tzinfo attribute of datetime object is not null.

Create datetime object with current time in local timezone

To create a datetime object with the current time in the local timezone, we need first to fetch the local timezone and then pass it to datetime.now() function. It will return the current time in the local timezone. For example,

from datetime import datetime
from dateutil import tz
import pytz

# Get local timezone
local_zone = tz.tzlocal()
# Create datetime object with current time in local timezone
dt_obj = datetime.now(tz = local_zone)

print(dt_obj)
print(dt_obj.tzinfo)

Output:

2021-10-17 12:09:14.172141+05:30
tzlocal()

It created a datetime object containing the current time in the local timezone. We confirmed it by checking that tzinfo attribute of the datetime object is not null.

Create datetime object with current time in a specific timezone

To create a datetime object with current time in a specific timezone, we can call the datetime.now() function with timezone information as the argument. For example,

from datetime import datetime
import pytz

dt_obj = datetime.now(tz = pytz.timezone('US/Alaska'))

print(dt_obj)
print(dt_obj.tzinfo)

Output:

2021-10-16 22:46:56.794783-08:00      
US/Alaska

It created a datetime object containing the current time in a specified timezone. We confirmed it by checking that tzinfo attribute of the datetime object is not null.

Create a naive datetime and add timezone information to it

You can also create a naive datetime object first. It will not contain any timezone information. It means tzinfo attribute of the datetime object will be null. Then you can assign the timezone to this naive datetime object later, to make it timezone aware datetime object. For example,

from datetime import datetime
import pytz

# Create datetime object with current time without timezone
dt_obj = datetime.now()

print(dt_obj)

# tzinfo of datetime object is null
print('tzinfo of datetime object : ', dt_obj.tzinfo)

# Assign the timezone to naive datetime object
dt_obj = pytz.timezone('US/Alaska').localize(dt_obj)

print(dt_obj)

# tzinfo of datetime object is not null
print('tzinfo of datetime object : ', dt_obj.tzinfo)

Output:

2021-10-17 12:26:31.670354
tzinfo of datetime object :  None     
2021-10-17 12:26:31.670354-08:00      
tzinfo of datetime object :  US/Alaska

tzinfo attribute of the datetime object was initially null. After we assigned a timezone to it using the localize() function, the tzinfo contained the timezone information.

Summary:

Today, we learned how to create timezone-aware datetime objects in Python or attach a timezone to the naïve datetime objects.

Pandas Tutorials -Learn Data Analysis with Python

   

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top