PYnative

Practice Coding

  • Python â–¼
    • Python Tutorials
    • Python Basics
    • Python Interview Q&As
  • Coding Exercises â–¼
    • Python Exercises
    • C++ Exercises
    • C Programming Exercises
    • Java Exercises
    • C# Exercises
  • MCQsâ–¼
    • Python MCQ Quizzes
    • Java MCQ Quizzes
    • C++ MCQ Quizzes
  • Compilersâ–¼
    • Online Python Compiler
    • Online C Compiler
    • Online C++ Compiler
    • Online Java Compiler
    • Online C# Compiler
Home » Python » Python DateTime » Working With TimeZones in Python

Working With TimeZones in Python

Updated on: December 1, 2026 | 3 Comments

In this tutorial, you’ll learn how to work with timezones in Python. Timezones matter as soon as your program deals with users, servers, or events in more than one place, for example a meeting scheduler, a flight booking site, or logs collected from servers around the world.

This article is part of a series on Python Date and Time.

After reading this article, you’ll learn:

  • The difference between timezone-naive and timezone-aware datetimes.
  • How to create a timezone-aware datetime using the built-in zoneinfo module and the timezone class.
  • How to get the current time in a different timezone.
  • How to get the timezone name, UTC offset, and DST offset of a datetime.
  • How to convert a datetime from one timezone to another.
  • How daylight saving time affects calculations, and how to avoid common mistakes.
  • How to use the third-party pytz library, which you’ll still see in older code.

Table of contents

  • What is timezone in Python?
  • Create Timezone Aware Datetime Object
    • Make an Existing Naive Datetime Timezone Aware
    • Format a Datetime to Show the Timezone Name
    • Create TimeZone Aware Datetime Object Using timezone class
  • Get Current Time in Different Timezone
  • Get TimeZone Information Using tzinfo
  • Converting Between Timezones
  • Working with Local Timezones
  • Daylight Saving Time Pitfalls
  • Compare and Subtract Aware and Naive Datetimes
  • Using the pytz Library
  • Summary

What is timezone in Python?

A timezone is a region of the world that uses the same standard time. Each timezone is described by its offset from UTC (Coordinated Universal Time), the world’s reference time. For example, India is 5 hours 30 minutes ahead of UTC, written as UTC+05:30.

Many timezones also use daylight saving time (DST): they move their clocks forward by an hour in summer. So the offset of a place can change during the year. For example, US Central Time is UTC-6 in winter (CST) and UTC-5 in summer (CDT).

UTC OffsetAbbreviationTimezone name
UTC+09:00JST (no DST)Asia/Tokyo
UTC+05:30IST (no DST)Asia/Kolkata
UTC+00:00 / UTC+01:00GMT in winter / BST in summerEurope/London
UTC-06:00 / UTC-05:00CST in winter / CDT in summerAmerica/Chicago
UTC-10:00HST (no DST)Pacific/Honolulu

In Python, timezone information is stored in a tzinfo object attached to a datetime. tzinfo is an abstract base class, so you don’t use it directly. Instead, you use one of its implementations:

  • datetime.timezone: a fixed offset from UTC, such as UTC or UTC+09:00. It doesn’t know about DST.
  • zoneinfo.ZoneInfo (Python 3.9+): a real-world timezone from the IANA database, such as 'America/Chicago'. It applies DST rules automatically. This is the recommended choice.
  • pytz: a third-party library that was the standard before Python 3.9.

Note: On Windows, run pip install tzdata once so zoneinfo can find the timezone data. Linux and macOS already include it.

Create Timezone Aware Datetime Object

A datetime object can be naive or aware:

  • A naive datetime has no timezone information (its tzinfo is None). datetime.now() and datetime(2026, 7, 1, 9, 0) both create naive datetimes. Python doesn’t know which place’s 9:00 they mean.
  • An aware datetime has a tzinfo, so it represents one exact moment in time.

Follow the steps below to create a timezone-aware datetime object in Python:

  1. Import ZoneInfo with from zoneinfo import ZoneInfo.
  2. Create a timezone object with its name, for example ZoneInfo('America/Chicago').
  3. Pass it to datetime.now(tz) for the current time, or as the tzinfo argument when you build a specific datetime.
Create timezone aware datetime

Example:

from datetime import datetime, timezone
from zoneinfo import ZoneInfo

# naive: no timezone
naive = datetime(2026, 7, 1, 9, 0)
print('Naive:', naive, '| tzinfo:', naive.tzinfo)

# aware: UTC
aware_utc = datetime(2026, 7, 1, 9, 0, tzinfo=timezone.utc)
print('UTC aware:', aware_utc)

# aware: US Central time
aware_central = datetime(2026, 7, 1, 9, 0, tzinfo=ZoneInfo('America/Chicago'))
print('US Central aware:', aware_central)Code language: Python (python)

Output:

Naive: 2026-07-01 09:00:00 | tzinfo: None
UTC aware: 2026-07-01 09:00:00+00:00
US Central aware: 2026-07-01 09:00:00-05:00
  • The UTC datetime ends with the offset +00:00.
  • The US Central datetime ends with -05:00, because on 1 July Chicago is on daylight saving time (CDT). In January, the same code would show -06:00.

For the current time, pass the timezone to datetime.now():

from datetime import datetime, timezone
from zoneinfo import ZoneInfo

print('Local naive:', datetime.now())
print('UTC:', datetime.now(timezone.utc))
print('US Central:', datetime.now(ZoneInfo('America/Chicago')))Code language: Python (python)

Output:

Local naive: 2026-12-01 12:01:24.844050
UTC: 2026-12-01 06:31:24.844072+00:00
US Central: 2026-12-01 00:31:24.844193-06:00

Your output will show the time when you run the program. Refer to list all timezones in Python if you don’t know the exact name of the timezone you need.

Make an Existing Naive Datetime Timezone Aware

If you already have a naive datetime and you know which timezone it belongs to, attach the timezone with replace(tzinfo=...). This keeps the date and time the same and only adds the timezone:

from datetime import datetime
from zoneinfo import ZoneInfo

naive = datetime(2026, 6, 15, 8, 45, 17)
print('Naive:', naive)

aware = naive.replace(tzinfo=ZoneInfo('Europe/Paris'))
print('Aware:', aware)Code language: Python (python)

Output:

Naive: 2026-06-15 08:45:17
Aware: 2026-06-15 08:45:17+02:00

Note: replace() returns a new datetime and leaves the original unchanged. It doesn’t convert the time. To convert a time into another timezone, use astimezone(), explained below.

Format a Datetime to Show the Timezone Name

Use the %Z directive of strftime() to show the timezone abbreviation, and %z to show the UTC offset:

from datetime import datetime
from zoneinfo import ZoneInfo

dt_india = datetime(2026, 7, 8, 17, 53, 23, tzinfo=ZoneInfo('Asia/Kolkata'))
print('Formatted DateTime in IST:', dt_india.strftime('%Y-%m-%d %H:%M:%S %Z %z'))Code language: Python (python)

Output:

Formatted DateTime in IST: 2026-07-08 17:53:23 IST +0530

Here, IST is the timezone abbreviation and +0530 is the offset from UTC.

Create TimeZone Aware Datetime Object Using timezone class

The datetime module’s timezone class (a subclass of tzinfo) represents a fixed offset from UTC. It needs no extra data, so it’s handy for UTC, or when you only know the offset and not the place.

Syntax:

datetime.timezone(offset, name=None)Code language: Python (python)
  • offset: a timedelta giving the difference from UTC. It must be strictly between -timedelta(hours=24) and timedelta(hours=24).
  • name: an optional name, returned by tzname().

Example:

from datetime import datetime, timezone, timedelta

# UTC
utc_dt = datetime(2026, 7, 1, 12, 0, tzinfo=timezone.utc)
print('UTC DateTime:', utc_dt)

# Japan Standard Time: UTC+9
jst = timezone(timedelta(hours=9), 'JST')
jst_dt = datetime(2026, 7, 1, 21, 0, tzinfo=jst)
print('In JST:', jst_dt, jst_dt.tzname())

# same moment?
print(utc_dt == jst_dt)Code language: Python (python)

Output:

UTC DateTime: 2026-07-01 12:00:00+00:00
In JST: 2026-07-01 21:00:00+09:00 JST
True

Both datetimes represent the same moment, so they compare as equal even though the clock times are different.

Note: A fixed offset doesn’t change for daylight saving time. Use timezone for UTC or fixed offsets, and ZoneInfo for real places like 'Europe/London'.

Get Current Time in Different Timezone

To get the current date and time in any timezone, pass a ZoneInfo object to datetime.now().

Syntax:

datetime.now(ZoneInfo('timezone name'))Code language: Python (python)

Tip: UTC is the common time standard across the world. Store and calculate times in UTC, and convert them to a local timezone only when you display them to a user.

In this example, we get the time in four US timezones and several other cities. We start from one fixed UTC time and convert it, so the output is predictable. Replace now_utc with datetime.now(timezone.utc) to see the current time:

from datetime import datetime, timezone
from zoneinfo import ZoneInfo

now_utc = datetime(2026, 7, 8, 13, 37, 34, tzinfo=timezone.utc)
fmt = '%Y-%m-%d %H:%M:%S %Z %z'

zones = {
    'US Eastern': 'America/New_York',
    'US Central': 'America/Chicago',
    'US Mountain': 'America/Denver',
    'US Pacific': 'America/Los_Angeles',
    'UK': 'Europe/London',
    'Germany': 'Europe/Berlin',
    'Japan': 'Asia/Tokyo',
    'Brazil': 'America/Sao_Paulo',
    'Australia': 'Australia/Sydney',
}

for label, name in zones.items():
    local = now_utc.astimezone(ZoneInfo(name))
    print(f'{label:12} {local.strftime(fmt)}')Code language: Python (python)

Output:

US Eastern   2026-07-08 09:37:34 EDT -0400
US Central   2026-07-08 08:37:34 CDT -0500
US Mountain  2026-07-08 07:37:34 MDT -0600
US Pacific   2026-07-08 06:37:34 PDT -0700
UK           2026-07-08 14:37:34 BST +0100
Germany      2026-07-08 15:37:34 CEST +0200
Japan        2026-07-08 22:37:34 JST +0900
Brazil       2026-07-08 10:37:34 -03 -0300
Australia    2026-07-08 23:37:34 AEST +1000

Notice -03 for Brazil: when a timezone has no common abbreviation, %Z shows the offset instead.

Get TimeZone Information Using tzinfo

An aware datetime can tell you about its timezone through three methods:

  • tzname(): returns the timezone abbreviation, for example 'CDT'.
  • utcoffset(): returns the total offset from UTC as a timedelta. It’s positive east of UTC and negative west of UTC, and it includes any DST adjustment.
  • dst(): returns the DST adjustment as a timedelta, usually 1 hour when DST is in effect and timedelta(0) otherwise. So utcoffset() - dst() gives the standard offset of the place.

Example: Compare US Central time in summer and in winter.

from datetime import datetime
from zoneinfo import ZoneInfo

central = ZoneInfo('America/Chicago')

for dt in (datetime(2026, 7, 8, 22, 30, tzinfo=central),
           datetime(2026, 1, 8, 22, 30, tzinfo=central)):
    print(dt.strftime('%Y-%m-%d %H:%M %Z'))
    print('  TimeZone name:', dt.tzname())
    print('  UTC offset:', dt.utcoffset())
    print('  DST:', dt.dst())
    print('  Hours from UTC:', dt.utcoffset().total_seconds() / 3600)Code language: Python (python)

Output:

2026-07-08 22:30 CDT
  TimeZone name: CDT
  UTC offset: -1 day, 19:00:00
  DST: 1:00:00
  Hours from UTC: -5.0
2026-01-08 22:30 CST
  TimeZone name: CST
  UTC offset: -1 day, 18:00:00
  DST: 0:00:00
  Hours from UTC: -6.0

A negative timedelta such as -5 hours is displayed as -1 day, 19:00:00 (minus one day plus 19 hours). Use total_seconds() / 3600 to get the offset in hours as a plain number.

Converting Between Timezones

Use the astimezone(tz) method to convert an aware datetime to another timezone. It returns a new datetime that represents the same moment, shown in the new timezone’s local time.

from datetime import datetime, timezone
from zoneinfo import ZoneInfo

fmt = '%Y-%m-%d %H:%M:%S %Z %z'

dt_utc = datetime(2026, 7, 9, 7, 19, 2, tzinfo=timezone.utc)
print('UTC DateTime:       ', dt_utc.strftime(fmt))

# UTC to US Central
dt_central = dt_utc.astimezone(ZoneInfo('America/Chicago'))
print('US Central DateTime:', dt_central.strftime(fmt))

# US Central to US Eastern
dt_eastern = dt_central.astimezone(ZoneInfo('America/New_York'))
print('US Eastern DateTime:', dt_eastern.strftime(fmt))

# US Eastern to India
dt_india = dt_eastern.astimezone(ZoneInfo('Asia/Kolkata'))
print('India DateTime:     ', dt_india.strftime(fmt))

print('Same moment?', dt_utc == dt_india)Code language: Python (python)

Output:

UTC DateTime:        2026-07-09 07:19:02 UTC +0000
US Central DateTime: 2026-07-09 02:19:02 CDT -0500
US Eastern DateTime: 2026-07-09 03:19:02 EDT -0400
India DateTime:      2026-07-09 12:49:02 IST +0530
Same moment? True

Calling astimezone() with no argument converts to your computer’s local timezone.

Note: If you call astimezone() on a naive datetime, Python assumes it’s in your computer’s local time. To avoid surprises, make the datetime aware first.

Working with Local Timezones

Your computer has a local timezone. Call astimezone() without an argument on any datetime to convert it to local time and find out the local timezone:

from datetime import datetime, timezone

local_now = datetime.now().astimezone()
print('Local time:', local_now)
print('Local timezone:', local_now.tzname(), local_now.utcoffset())

# convert a UTC time to local time
utc_dt = datetime(2026, 7, 1, 12, 0, tzinfo=timezone.utc)
print('UTC 12:00 in local time:', utc_dt.astimezone())Code language: Python (python)

Output:

Local time: 2026-12-01 12:01:24.972714+05:30
Local timezone: IST 5:30:00
UTC 12:00 in local time: 2026-07-01 17:30:00+05:30

This example ran on a computer set to India Standard Time, so your output will show your own timezone.

Daylight Saving Time Pitfalls

Daylight saving time causes two surprises that are worth knowing about.

1. Adding a timedelta works on the wall clock. In 2026, US clocks move forward one hour at 2:00 AM on 8 March. Adding 24 hours to a time in a ZoneInfo timezone keeps the same clock time on the next day, but only 23 real hours have passed. To add exact elapsed time, do the arithmetic in UTC:

from datetime import datetime, timedelta, timezone
from zoneinfo import ZoneInfo

ny = ZoneInfo('America/New_York')
start = datetime(2026, 3, 7, 12, 0, tzinfo=ny)

# wall-clock arithmetic
print(start + timedelta(hours=24))

# exact elapsed time: convert to UTC, add, convert back
exact = (start.astimezone(timezone.utc) + timedelta(hours=24)).astimezone(ny)
print(exact)Code language: Python (python)

Output:

2026-03-08 12:00:00-04:00
2026-03-08 13:00:00-04:00

2. Some local times happen twice. When clocks move back in autumn, the hour from 1:00 to 2:00 AM repeats. Python uses the fold attribute to pick one: fold=0 (the default) is the first occurrence, and fold=1 is the second:

from datetime import datetime
from zoneinfo import ZoneInfo

ny = ZoneInfo('America/New_York')

first = datetime(2026, 11, 1, 1, 30, tzinfo=ny)
second = datetime(2026, 11, 1, 1, 30, fold=1, tzinfo=ny)
print(first, first.tzname())
print(second, second.tzname())Code language: Python (python)

Output:

2026-11-01 01:30:00-04:00 EDT
2026-11-01 01:30:00-05:00 EST

Compare and Subtract Aware and Naive Datetimes

You can compare or subtract two aware datetimes even if they’re in different timezones. Python converts them to UTC first. But you can’t mix an aware datetime with a naive one:

from datetime import datetime, timezone
from zoneinfo import ZoneInfo

tokyo = datetime(2026, 7, 1, 18, 0, tzinfo=ZoneInfo('Asia/Tokyo'))
london = datetime(2026, 7, 1, 9, 0, tzinfo=ZoneInfo('Europe/London'))
print('Difference:', tokyo - london)

naive = datetime(2026, 7, 1, 9, 0)
print(naive - london)Code language: Python (python)

Output:

Difference: 1:00:00
TypeError: can't subtract offset-naive and offset-aware datetimes

6 PM in Tokyo is 09:00 UTC and 9 AM in London is 08:00 UTC (London is UTC+1 in summer), so the Tokyo time is 1 hour later. To fix the error, make the naive datetime aware with replace(tzinfo=...) before the calculation.

Using the pytz Library

Before Python 3.9, the third-party pytz library (pip install pytz) was the usual way to work with timezones, so you’ll find it in many existing projects. Its main functions are:

  • pytz.utc: the UTC timezone.
  • pytz.timezone('region'): creates the timezone object of a region.
  • tz.localize(naive_dt): attaches a pytz timezone to a naive datetime.
  • tz.normalize(dt): fixes the offset after arithmetic across a DST change.
from datetime import datetime
import pytz

tz = pytz.timezone('America/Chicago')

# current time in a timezone
print(datetime.now(tz))

# make a naive datetime aware: use localize(), not replace()
dt = tz.localize(datetime(2026, 7, 1, 9, 0))
print(dt)   # 2026-07-01 09:00:00-05:00

# convert to another timezone
print(dt.astimezone(pytz.timezone('Asia/Kolkata')))Code language: Python (python)

Important: With pytz, never use replace(tzinfo=pytz.timezone(...)) or pass a pytz timezone to the datetime() constructor. pytz then uses the city’s historical “Local Mean Time” offset, which is several minutes off (for example -05:51 for Chicago instead of -05:00). Always use localize(). zoneinfo doesn’t have this problem, which is one reason it’s now preferred.

Summary

TaskUse
Current time in a timezonedatetime.now(ZoneInfo('Asia/Tokyo'))
Current UTC timedatetime.now(timezone.utc)
Attach a timezone to a naive datetimedt.replace(tzinfo=ZoneInfo(...))
Convert to another timezonedt.astimezone(ZoneInfo(...))
Convert to local timedt.astimezone()
Fixed UTC offsettimezone(timedelta(hours=9))
Timezone name / offset / DSTdt.tzname(), dt.utcoffset(), dt.dst()

Filed Under: Python, Python DateTime

Did you find this page helpful? Let others know about it. Sharing helps me continue to create free Python resources.

TweetF  sharein  shareP  Pin

About Vishal

I’m Vishal Hule, the Founder of PYnative.com. As a Python developer, I enjoy assisting students, developers, and learners. Follow me on Twitter.

Related Tutorial Topics:

Python Python DateTime

All Coding Exercises:

C Exercises
C++ Exercises
Python Exercises
Java Exercises
C# Exercises

Python Exercises and Quizzes

Free coding exercises and quizzes cover Python basics, data structure, data analytics, and more.

  • 15+ Topic-specific Exercises and Quizzes
  • Each Exercise contains 25+ questions
  • Each Quiz contains 25 MCQ
Exercises
Quizzes
In: Python Python DateTime
TweetF  sharein  shareP  Pin

 Python DateTime

  • Python DateTime Guide
  • Python Get Current DateTime
  • Python DateTime Formatting
  • Python String to DateTime
  • Python Timestamp
  • Python Timedelta
  • Python TimeZones
  • List All TimeZones in Python

 Explore Python

  • Python Tutorials
  • Python Exercises
  • Python Quizzes
  • Python Interview Q&A
  • Python Programs

All Python Topics

Python Basics Python Exercises Python Quizzes Python Interview Python File Handling Python OOP Python Date and Time Python Random Python Regex Python Pandas Python Databases Python MySQL Python PostgreSQL Python SQLite Python JSON

About PYnative

PYnative.com is a platform built to help developers learn by doing. Solve coding exercises, take MCQs, and run code directly in built-in online compilers.

Follow Us

To get New Python Tutorials, Exercises, and Quizzes

  • Twitter
  • Facebook
  • Sitemap

Explore Python

  • Learn Python
  • Python Basics
  • Python Databases
  • Python Exercises
  • Python Quizzes
  • Online Python Code Editor
  • Python Tricks

Coding Exercises

  • C Exercises
  • C++ Exercises
  • Python Exercises
  • Java Exercises
  • C# Exercises

MCQ Quizzes

  • Python MCQs
  • Java MCQs
  • C++ MCQs

Legal Stuff

  • About Us
  • Contact Us

We use cookies to improve your experience. While using PYnative, you agree to have read and accepted our:

  • Terms Of Use
  • Privacy Policy
  • Cookie Policy

Copyright © 2018–2026 pynative.com