Python: Get the Current Date and Time with Timezone

Updated: January 17, 2023 By: Wolf Post a comment

In Python, you can get the current date and time in a particular time zone by using the datetime.now() method (recommended) or the datetime.fromtimestamp() method and specify the tz argument to a valid time zone object (an instance of a tzinfo subclass). Explaining things in words is quite vague and confusing; let’s examine the following examples for a better understanding.

Setting time zone with the zoneinfo module

This example works with Python 3.9 or later. We’ll use the zoneinfo module (a concrete implementation of the datetime.tzinfo abstract base class) to easily provide the time zone information.

Example:

from datetime import datetime
import time
from zoneinfo import ZoneInfo

# US Pacific Time
us_eastern_dt = datetime.now(tz=ZoneInfo("America/New_York"))
print(us_eastern_dt)

# US Western Time
us_western_dt = datetime.fromtimestamp(time.time(), tz=ZoneInfo("America/Los_Angeles"))
print(us_western_dt)

# Turkey Time
turkey_dt = datetime.now(tz=ZoneInfo("Europe/Istanbul"))
print(turkey_dt)

# South Korea Time
korea_dt = datetime.now(tz=ZoneInfo("Asia/Seoul"))
print(korea_dt)

# India Time
india_dt = datetime.now(tz=ZoneInfo("Asia/Kolkata"))
print(india_dt)

# Vietnam Time
vietnam_dt = datetime.now(tz=ZoneInfo("Asia/Ho_Chi_Minh"))
print(vietnam_dt)

Output:

2023-01-17 05:19:22.257891-05:00
2023-01-17 02:19:22.258190-08:00
2023-01-17 13:19:22.258302+03:00
2023-01-17 19:19:22.258408+09:00
2023-01-17 15:49:22.258500+05:30
2023-01-17 17:19:22.258597+07:00

Note that the results you receive depend on when you run the code. If you want to print out the results in a nicer format, see this article: How to Format Date and Time in Python.

Specifying time zone with the pytz module

If you are working with an old version of Python (prior to 3.9), you can set the time zone by using the pytz module. This module needs to be installed first:

pip install pytz

Example:

from datetime import datetime
import time
import pytz

# Germany time
germany_time = datetime.now(pytz.timezone('Europe/Berlin'))
print(germany_time)

# Egypt time
egypt_time = datetime.fromtimestamp(time.time(), tz=pytz.timezone('Africa/Cairo'))
print(egypt_time)

# Pakistan time
pakistan_time = datetime.now(pytz.timezone('Asia/Karachi'))
print(pakistan_time)

# China time
china_time = datetime.now(pytz.timezone('Asia/Shanghai'))
print(china_time)

# Japan time
japan_time = datetime.now(tz=pytz.timezone('Asia/Tokyo'))
print(japan_time)

# Indonesia time
indonesia_time = datetime.fromtimestamp(time.time(), tz=pytz.timezone('Asia/Jakarta'))
print(indonesia_time)

# Australia time
australia_time = datetime.now(tz=pytz.timezone('Australia/Sydney'))
print(australia_time)

My output:

2023-01-17 11:29:02.126002+01:00
2023-01-17 12:29:02.126031+02:00
2023-01-17 15:29:02.126309+05:00
2023-01-17 18:29:02.126392+08:00
2023-01-17 19:29:02.126546+09:00
2023-01-17 17:29:02.126563+07:00
2023-01-17 21:29:02.126865+11:00

Specifying UTC offset

You can also set a timezone by UTC offset. In my opinion, this method has a small advantage in that you will have a harder time getting typos than the 2 approaches above.

Example:

from datetime import datetime, timedelta, timezone

# UTC + 5:00 (Pakistan, India, Bangladesh, Nepal, Sri Lanka, and Bhutan time zone)
dt1 = datetime.now(tz=timezone(timedelta(hours=5)))
print(dt1)

# UTC + 7:00 (Vietnam, Thailand, and Indonesia time zone)
dt2 = datetime.now(tz=timezone(timedelta(hours=7)))
print(dt2)

# UTC - 3:00 (Argentina, Brazil, Chile, Paraguay, Uruguay, and Falkland Islands time zone)
dt3 = datetime.now(tz=timezone(timedelta(hours=-3)))
print(dt3)

Output:

2023-01-17 15:35:55.151041+05:00
2023-01-17 17:35:55.151252+07:00
2023-01-17 07:35:55.151259-03:00