Convert datetime Object to Local Time Zone in Python (Example)

 

In this article, I’ll demonstrate how to convert a datetime object to the local time zone in the Python programming language.

Table of content:

Let’s dive straight into the Python code!

 

Introduction – datetime Module & Example Data

datetime is a module in python which can be used to create and manipulate the date and time related data.

We can import datetime from the datetime module.

Creating Example Data

In this tutorial, we are going to get the current date and time from the datetime module. First, we have to import the datetime module to Python:

from datetime import datetime

Furthermore, we have to import the pytz module:

import pytz

Next, we can create a datetime object using the Python code below:

my_datetime = datetime(2024, 1, 15, 16, 20, 13, tzinfo = pytz.utc)
print(my_datetime)
# 2024-01-15 16:20:13+00:00

The example date and time of this tutorial is 2024-01-15 16:20:13+00:00.

We can use the strftime function to display the time zone that is used for this datetime object:

my_datetime_utc = my_datetime.strftime('%Y-%m-%d %H:%M:%S %Z%z')
print(my_datetime_utc)
# 2024-01-15 16:20:13 UTC+0000

As you can see, the time zone of our datetime object is the UTC time zone (Coordinated Universal Time).

Let’s transform our datetime object to our local time zone!

Example: Using astimezone()

This example uses the astimezone() function to convert our datetime object into local time.

We can return the local time zone using the code below:

import datetime
local_time = datetime.datetime.now().astimezone().tzinfo
print(local_time)
# Central European Time

In my case, the local time zone is CET (i.e. Central European Time).

If we want to convert our example datetime object to CET, we can use the Python code below:

my_datetime_cet = my_datetime.astimezone(pytz.timezone('Europe/Berlin')).strftime('%Y-%m-%d %H:%M:%S %Z%z')
print(my_datetime_cet)
# 2024-01-15 17:20:13 CET+0100

The CET time zone is one hour earlier than the UTC time zone, i.e. 2024-01-15 17:20:13 CET+0100.

 

Video, Further Resources & Summary

If you like to know more about how to convert a datetime object to the local time zone in Python, then you may have a look at the following video of the YouTube channel from PyLenin.

In the video, the author speaks about dealing with time zones and the unix timestamp in Python.

 

 

Furthermore, you can also take a look at our other tutorials on the Statistics Globe website:

Summary: This post has illustrated how to switch a datetime object to a local time zone in the Python programming language.

In case you have any additional questions, you may leave a comment below.

Furthermore, you may subscribe to the Statistics Globe newsletter to get regular updates on the latest news on Statistics Globe. Check out the subscription box below for more details.

 

Gottumukkala Sravan Kumar Statistician & Programmer

Note: This article was created in collaboration with Gottumukkala Sravan Kumar. You may find more information about Gottumukkala and his other articles on his profile page.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top