Get Current Time in Specific Timezone in Python (Example)

 

In this Python programming tutorial you’ll learn how to get timezone aware value from datetime.today.

Table of contents:

Let’s get started.

 

Example Data & Libraries

First, we have to load the datetime library:

import datetime                                  # Load datetime

We can return the time of today in the timezone of our current location using the today() function:

date_today = datetime.datetime.today()           # Create datetime object for today
print(date_today)                                # Print today
# 2022-03-25 11:33:45.477931

As you can see based on the previous output of the Python console, our current time is the 25th of March 2022, 11:33 am.

Let’s assume that we want to return the current date and time of a different timezone!

 

Example: Get Timezone Aware Value from datetime.today()

This example explains how to get the current time in a different timezone.

For this, we need to load the pytz library:

import pytz                                      # Import pytz

Next, we can apply the datetime.now function and the pytz.timezone functions as shown below:

date_today_UCT = datetime.datetime.now(pytz.timezone('UCT'))  # Specify timezone manually
print(date_today_UCT)                            # Print datetime with manual timezone
# 2022-03-25 10:33:45.477931+00:00

The previous Python syntax has returned the current date and time in the UCT timezone.

 

Video & Further Resources

If you need further info on the Python syntax of this post, you could have a look at the following video on my YouTube channel. I illustrate the content of this tutorial in the video:

 

The YouTube video will be added soon.

 

Furthermore, you may want to have a look at the other articles on my homepage. You can find some articles below:

 

In summary: In this tutorial you have learned how to return current time from a specific timezone in the Python programming language. If you have further questions, don’t hesitate to let me know in the comments.

 

Matthias Bäuerlen Python Programmer

This page was created in collaboration with Matthias Bäuerlen. Have a look at Matthias’ author page to get more information about his professional background, a list of all his tutorials, as well as an overview on his other tasks on Statistics Globe.

 

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.

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

Menu
Top