Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to convert date and time with different timezones in Python?
In this article, we will show you how to convert date and time with different timezones in Python using the pytz library.
Using astimezone() function
Using datetime.now() function
The easiest way in Python date and time to handle timezones is to use the pytz module. This library allows accurate and cross?platform timezone calculations and brings the Olson tz database into Python.
Before you use it you'll need to install it using ?
pip install pytz
Using astimezone() Function
The astimezone() method converts a datetime object from one timezone to another. It takes a timezone object as an argument and returns a new datetime object in the specified timezone.
Example
The following program converts the current UTC time to Asia/Kolkata timezone ?
# importing datetime
from datetime import datetime
# importing timezone from pytz module
from pytz import timezone
# giving the format of datetime
format = "%Y-%m-%d %H:%M:%S %Z%z"
# getting the current time in UTC timezone
now_utc = datetime.now(timezone('UTC'))
# Format the above DateTime using the strftime()
print('Current Time in UTC TimeZone:', now_utc.strftime(format))
# Converting to Asia/Kolkata time zone
now_asia = now_utc.astimezone(timezone('Asia/Kolkata'))
# Format the above datetime using the strftime()
print('Current Time in Asia/Kolkata TimeZone:', now_asia.strftime(format))
Current Time in UTC TimeZone: 2022-09-07 04:23:21 UTC+0000 Current Time in Asia/Kolkata TimeZone: 2022-09-07 09:53:21 IST+0530
Using datetime.now() Function
You can also get the current time directly in different timezones by passing the timezone object to datetime.now().
Example
The following program gets the current time in two different timezones ?
# importing datetime
from datetime import datetime
# importing pytz module
import pytz
# giving the format of datetime
format = "%Y-%m-%d %H:%M:%S %Z%z"
# getting the standard UTC time
original_tz = pytz.timezone('Asia/Kolkata')
# giving the timezone to which it is to be converted
converted_tz = pytz.timezone('US/Eastern')
# Getting the current time in the Asia/Kolkata Time Zone
datetime_object = datetime.now(original_tz)
# Format the above datetime using the strftime()
print("Original Date & Time: in Asia/Kolkata ", datetime_object.strftime(format))
# Getting the current time in the US/Eastern Time Zone
datetime_object = datetime.now(converted_tz)
# Format the above datetime using the strftime()
print("Converted Date & Time: in US/Eastern TimeZone ", datetime_object.strftime(format))
Original Date & Time: in Asia/Kolkata 2022-09-07 10:00:49 IST+0530 Converted Date & Time: in US/Eastern TimeZone 2022-09-07 00:30:49 EDT-0400
Converting Existing Datetime
You can also convert an existing datetime string to different timezones ?
from datetime import datetime
import pytz
# Parse a datetime string
date_string = "2022-09-07 15:30:00"
naive_datetime = datetime.strptime(date_string, "%Y-%m-%d %H:%M:%S")
# Localize to a specific timezone
utc_tz = pytz.timezone('UTC')
localized_datetime = utc_tz.localize(naive_datetime)
print("Original UTC time:", localized_datetime)
# Convert to different timezones
tokyo_tz = pytz.timezone('Asia/Tokyo')
tokyo_time = localized_datetime.astimezone(tokyo_tz)
print("Tokyo time:", tokyo_time)
Original UTC time: 2022-09-07 15:30:00+00:00 Tokyo time: 2022-09-08 00:30:00+09:00
Comparison
| Method | Use Case | Best For |
|---|---|---|
astimezone() |
Converting existing timezone-aware datetime | Converting between known timezones |
datetime.now(tz) |
Getting current time in specific timezone | Getting current time in different zones |
localize() |
Adding timezone info to naive datetime | Converting timezone-naive to timezone-aware |
Conclusion
Use astimezone() to convert between timezones and datetime.now(timezone) to get current time in specific timezones. The pytz library provides accurate timezone handling for Python applications.
