Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to make a timezone aware datetime object in Python

  • Difficulty Level : Hard
  • Last Updated : 26 Sep, 2021

In this example, we are going to see how to make a timezone-aware DateTime object in Python.

Timezone-aware objects are Python DateTime or time objects that include timezone information. An aware object represents a specific moment in time that is not open to interpretation.

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

Checking if an object is timezone aware or not: 

We can easily check if a datetime object is timezone-aware or not.  For this, we will store the current date and time in a new variable using the datetime.now() function of datetime module.



Syntax: datetime.now(tz)

Parameters: tz : Specified time zone of which current time and date is required. (Uses Greenwich Meridian time by default.)

Then we will check the timezone information of the object stored in the tzinfo base class. tzinfo is an abstract base class for time zone information objects.

Python3




# Importing the datetime module
import datetime
 
# Storing the current date and time in
# a new variable using the datetime.now()
# function of datetime module
current_date = datetime.datetime.now()
 
# Checking the timezone information of the
# object stored in tzinfo base class
if current_date.tzinfo == None or current_date.tzinfo.\
        utcoffset(current_date) == None:
   
    # If passes the above condition then
    # the object is unaware
    print("Unaware")
else:
   
    # Else printing "Aware"
    print("Aware")

Output:

Unaware

Timezone aware object using datetime

For this, we will store the current time in a new variable using the datetime.now().time() function of datetime module. Then we will replace the value of the timezone in the tzinfo class of the object using the replace() function. After that convert the date value into ISO 8601 format using the isoformat() method.

Syntax: isoformat(sep=’T’, timespec=’auto’)

Parameters:

  • sep: It is a one character separator placed between date and time.
  • timespec: Number of additional component of time to include

Code:



Python3




# Importing the datetime module
import datetime
 
# Storing the current date and time in
# a new variable using the datetime.now()
# function of datetime module
current_date = datetime.datetime.now()
 
# Replacing the value of the timezone in tzinfo class of
# the object using the replace() function
current_date = current_date.\
    replace(tzinfo=datetime.timezone.utc)
 
# Converting the date value into ISO 8601
# format using isoformat() method
current_date = current_date.isoformat()
 
# Printing the value of current_date
print(current_date)

Output:

2021-08-30T09:45:43.291212+00:00

Now let’s check if the object is timezone aware or not using the method we used in the 1st section of the article.

Python3




# Importing the datetime module
import datetime
 
# Storing the current date and time in
# a new variable using the datetime.now()
# function of datetime module
current_date = datetime.datetime.now()
 
# Replacing the value of the timezone in tzinfo class of
# the object using the replace() function
current_date = current_date.replace(tzinfo=datetime.timezone.utc)
 
# Checking the timezone information of the
# object stored in tzinfo base class
if current_date.tzinfo == None or \
current_date.tzinfo.utcoffset(current_date) == None:
   
    # If passes the above condition then
    # the object is unaware
    print("Unaware")
else:
   
    # Else printing "Aware"
    print("Aware")
 
# Converting the date value into ISO 8601
# format using isoformat() method
current_date = current_date.isoformat()
 
# Printing the value of current_date
print(current_date)

Output:

Aware
2021-08-30T09:55:15.111556+00:00

Timezone aware object using pytz

You can also use the pytz module to create timezone-aware objects.

For this, we will store the current date and time in a new variable using the datetime.now() function of datetime module and then we will add the timezone using timezone function of pytz module.

Python3




# Importing the datetime module
import datetime
import pytz
 
# Storing the current date and time in
# a new variable using the datetime.now()
# function of datetime module and adding the timezone
# using timezone function of pytz module.
current_date = datetime.datetime.now(pytz.timezone('Africa/Abidjan'))
 
# Printing the value of current_date
print(current_date)

Output:

2021-08-30 04:35:37.036990+00:00

Now let’s check if the object is timezone aware or not using the method we used in the 1st section of the article.

Python3




# Importing the datetime module
import datetime
import pytz
 
# Storing the current date and time in
# a new variable using the datetime.now()
# function of datetime module and adding the timezone
# using timezone function of pytz module.
current_date = datetime.datetime.now(pytz.timezone('Africa/Abidjan'))
 
# Checking the timezone information of the
# object stored in tzinfo base class
if current_date.tzinfo == None or current_date.\
tzinfo.utcoffset(current_date)== None:
   
    # If passes the above condition then
    # the object is unaware
    print("Unaware")
else:
    # Else printing "Aware"
    print("Aware")
     
# Printing the value of current_date
print(current_date)

Output:

Aware
2021-08-30 04:46:40.670455+00:00



My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!