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:
Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.
If you accept this notice, your choice will be saved and the page will refresh.
If you are eager to learn more about datetime objects in general, please have a look here.
Furthermore, you may want to have a look at the other articles on my homepage. You can find some articles below:
- Convert datetime to Different Time Zone in Python
- Get Current Time in Specific Timezone in Python
- All Python Programming Tutorials
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.
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.