Sub topic 8
Converting between time zones
This chapter assumes you can create aware datetimes with ZoneInfo, as covered in the previous chapter. It also assumes you know that an aware datetime carries a UTC offset. Here you will learn how astimezone moves a moment between zones. You will also see how it differs from replace, and what happens at daylight saving boundaries.
One instant, many wall clocks
An aware datetime names one instant in time. It also shows that instant on one particular wall clock. Converting between zones keeps the instant and changes only the wall clock.
datetime.astimezone(tz) does this. It returns a new datetime that represents the same instant, expressed in the zone tz.
from datetime import datetime, timezone
from zoneinfo import ZoneInfo
meeting = datetime(2026, 11, 26, 9, 0, tzinfo=ZoneInfo("America/New_York"))
print(meeting.astimezone(timezone.utc))
print(meeting.astimezone(ZoneInfo("Asia/Tokyo")))2026-11-26 14:00:00+00:00
2026-11-26 23:00:00+09:00New York is on standard time (UTC-5) in late November, so 09:00 there is 14:00 UTC. Tokyo is UTC+9 all year, so the same instant is 23:00 there.
Internally, astimezone takes three steps:
- It subtracts the source offset to get UTC.
- It asks the target zone for the offset that applies at that UTC instant.
- It adds that offset and attaches the target
tzinfo.
Because the offset is looked up for the specific instant, the same target zone can apply different offsets on different dates. You never have to track DST yourself.
astimezone versus replace
replace(tzinfo=...) and astimezone(...) both change the zone, but they do different things. replace keeps the wall-clock fields and swaps the label, which changes the instant. astimezone keeps the instant and recomputes the fields.
from datetime import datetime
from zoneinfo import ZoneInfo
dt = datetime(2026, 11, 26, 9, 0, tzinfo=ZoneInfo("America/New_York"))
kolkata = ZoneInfo("Asia/Kolkata")
relabelled = dt.replace(tzinfo=kolkata)
converted = dt.astimezone(kolkata)
print(relabelled)
print(converted)
print(relabelled == converted)2026-11-26 09:00:00+05:30
2026-11-26 19:30:00+05:30
FalseThe first result is a different moment, 09:00 in India instead of 09:00 in New York. The second is the same moment as dt.
| Operation | Changes the instant? | Changes the wall-clock fields? | Use it when |
|---|---|---|---|
| `dt.astimezone(tz)` | No | Yes | Showing an existing moment in another zone |
| `dt.replace(tzinfo=tz)` | Yes | No | Attaching a zone to a naive time you know is local to `tz` |
Use replace(tzinfo=...) on a naive datetime only when you know which zone its wall-clock reading belongs to. Never use it to convert an aware datetime.
Showing one moment in several zones
A common task is showing one event to people in different places. Keep the stored value in UTC and convert it once per viewer.
from datetime import datetime, timezone
from zoneinfo import ZoneInfo
event = datetime(2026, 11, 26, 15, 0, tzinfo=timezone.utc)
zones = [
"America/Los_Angeles",
"America/New_York",
"Europe/London",
"Asia/Kolkata",
"Australia/Sydney",
]
for name in zones:
local = event.astimezone(ZoneInfo(name))
print(f"{name:<20} {local:%Y-%m-%d %H:%M %Z}")America/Los_Angeles 2026-11-26 07:00 PST
America/New_York 2026-11-26 10:00 EST
Europe/London 2026-11-26 15:00 GMT
Asia/Kolkata 2026-11-26 20:30 IST
Australia/Sydney 2026-11-27 02:00 AEDTSydney lands on the next calendar day. The date changes along with the hour, so never format only the time when you show a converted value to someone far away. Sydney is also on daylight time (UTC+11) in November, because its summer is our winter.
Naive datetimes and astimezone
astimezone also accepts a naive datetime. Python then assumes the naive value is in the system's local time zone. The result depends on the machine that runs the code.
from datetime import datetime, timezone
naive = datetime(2026, 11, 26, 9, 0)
print(naive.astimezone(timezone.utc)) # the output depends on the machine's local zoneOn a laptop set to New York this prints 14:00 UTC. On a server set to UTC it prints 09:00 UTC. Both runs succeed without any error, so the bug can reach production unnoticed.
A small guard at your boundaries avoids this:
from datetime import datetime
from zoneinfo import ZoneInfo
def to_zone(dt: datetime, zone_name: str) -> datetime:
"""Convert an aware datetime to the named IANA zone."""
if dt.utcoffset() is None:
raise ValueError("expected an aware datetime, got a naive one")
return dt.astimezone(ZoneInfo(zone_name))
try:
to_zone(datetime(2026, 11, 26, 9, 0), "Asia/Tokyo")
except ValueError as e:
print(e)expected an aware datetime, got a naive oneThe check uses utcoffset() is None rather than tzinfo is None. A datetime can have a tzinfo object that still returns no offset, and the Python documentation counts that as naive.
Calling astimezone with no argument
dt.astimezone() with no argument, or with None, converts to the system's local zone. The result carries a fixed-offset timezone object, not a ZoneInfo. It records the offset in force at that instant but knows nothing about DST rules.
This is acceptable for a quick display on the user's own machine. Do not use it on a server. The server's zone is rarely the viewer's zone, and the fixed offset makes later arithmetic unreliable. To show a viewer's local time, store their IANA zone name and convert with ZoneInfo(name).
Fixed offsets as targets
The target of astimezone can be any tzinfo, including a fixed-offset timezone:
from datetime import datetime, timedelta, timezone
dt = datetime(2026, 11, 26, 14, 0, tzinfo=timezone.utc)
ist = timezone(timedelta(hours=5, minutes=30))
print(dt.astimezone(ist))2026-11-26 19:30:00+05:30This is correct for that one moment, but a fixed offset never changes. If you add a day across a DST change, the result still uses the old offset. Prefer a ZoneInfo whenever the value represents a place.
Daylight saving boundaries
Conversion is always safe in one direction and can be problematic in the other:
- UTC to local is unambiguous. Every UTC instant maps to exactly one local reading.
- Local to UTC can be ambiguous, because some local times occur twice or not at all.
From UTC to local
When astimezone produces an ambiguous local time, it sets fold for you, so the result stays correct.
from datetime import datetime, timezone
from zoneinfo import ZoneInfo
ny = ZoneInfo("America/New_York")
for hour in (5, 6):
utc = datetime(2026, 11, 1, hour, 30, tzinfo=timezone.utc)
local = utc.astimezone(ny)
print(utc, local, local.tzname(), local.fold)2026-11-01 05:30:00+00:00 2026-11-01 01:30:00-04:00 EDT 0
2026-11-01 06:30:00+00:00 2026-11-01 01:30:00-05:00 EST 1Two UTC instants one hour apart both display as 01:30 in New York. They differ in offset and in fold. Comparing, sorting, and converting back to UTC all keep them distinct.
From local to UTC
When you convert a local time that is ambiguous or nonexistent, the datetime's fold value decides the outcome. For a repeated time, fold=0 picks the first occurrence and fold=1 the second. For a nonexistent time in a spring-forward gap, fold=0 uses the offset from before the change, and fold=1 uses the offset from after it.
from datetime import datetime, timezone
from zoneinfo import ZoneInfo
ny = ZoneInfo("America/New_York")
# 02:30 on 2026-03-08 never happens: clocks jump from 02:00 to 03:00.
gap = datetime(2026, 3, 8, 2, 30, tzinfo=ny)
for candidate in (gap, gap.replace(fold=1)):
as_utc = candidate.astimezone(timezone.utc)
print(as_utc, "->", as_utc.astimezone(ny))2026-03-08 07:30:00+00:00 -> 2026-03-08 03:30:00-04:00
2026-03-08 06:30:00+00:00 -> 2026-03-08 01:30:00-05:00Neither answer is "right", because the input time never existed. A round trip through UTC moves the wall-clock value to a time that did exist. If your program accepts user-typed local times, check for this case instead of silently accepting it. One approach is to convert to UTC and back, then compare the wall-clock fields with the original:
from datetime import datetime, timezone
from zoneinfo import ZoneInfo
def exists(local: datetime) -> bool:
"""Return True if this aware local time is a real wall-clock reading."""
round_trip = local.astimezone(timezone.utc).astimezone(local.tzinfo)
return round_trip.replace(tzinfo=None) == local.replace(tzinfo=None)
ny = ZoneInfo("America/New_York")
print(exists(datetime(2026, 3, 8, 2, 30, tzinfo=ny)))
print(exists(datetime(2026, 3, 8, 3, 30, tzinfo=ny)))False
TrueThis check cannot detect repeated times, since both occurrences round-trip to themselves. For those you must decide how to set fold, for example by asking the user or by always taking the first occurrence.
Comparing and subtracting across zones
Comparing two aware datetimes with different tzinfo objects compares instants. ==, <, and subtraction all behave as you would hope.
There is one trap. If both operands share the same tzinfo object, Python ignores the offsets and compares wall-clock fields. Subtraction across a DST change in a single zone therefore reports wall-clock difference:
from datetime import datetime, timezone
from zoneinfo import ZoneInfo
ny = ZoneInfo("America/New_York")
start = datetime(2026, 3, 7, 12, 0, tzinfo=ny)
end = datetime(2026, 3, 8, 12, 0, tzinfo=ny)
print(end - start)
print(end.astimezone(timezone.utc) - start.astimezone(timezone.utc))1 day, 0:00:00
23:00:00Only 23 real hours passed, because the clocks skipped an hour on March 8. When elapsed time matters, convert both values to UTC before subtracting. The next chapters cover this in more detail.
Two further rules apply to equality. First, an ambiguous or nonexistent time in one zone never compares equal to a datetime in a different zone, even if the offsets happen to match. Second, a fold=0 and a fold=1 datetime that share a tzinfo object compare equal, because that comparison uses wall-clock fields only.
Errors to expect
| Error | Cause | Fix |
|---|---|---|
| `ZoneInfoNotFoundError` | The key is misspelled, or no time zone database is available | Check the name, and see the tzdata chapter |
| `ValueError` for a key | The key is malformed, such as an absolute path or `..` | Use only IANA names like `Europe/Paris` |
| `TypeError` on compare or subtract | One side is naive and the other is aware | Make both aware, as shown earlier |
| `OverflowError` | Conversion moves a value past `datetime.min` or `datetime.max` | Avoid converting values near the limits |
Summary
astimezone(tz)keeps the instant and changes the wall clock.replace(tzinfo=tz)keeps the wall clock and changes the instant.- Never call
astimezoneon a naive value. It silently assumes the machine's local zone. - UTC to local conversion is always unambiguous and sets
foldfor you. Local to UTC conversion can be ambiguous or hit a gap, andfolddecides the result. - Convert to UTC before subtracting when you need real elapsed time.
Exercise
A call is scheduled for 2026-07-04 18:00 in London (Europe/London). What aware datetime, in America/New_York, represents the same instant? Write the code and the printed result.
Show answer
Attach the London zone, then convert. from datetime import datetime from zoneinfo import ZoneInfo call = datetime(2026, 7, 4, 18, 0, tzinfo=ZoneInfo("Europe/London")) print(call.astimezone(ZoneInfo("America/New_York"))) London is on British Summer Time (UTC+1) in July, so the instant is 17:00 UTC. New York is on daylight time (UTC-4), so the output is 2026-07-04 13:00:00-04:00.
Exercise
A colleague writes dt.replace(tzinfo=ZoneInfo("Asia/Tokyo")) to "show an aware New York datetime in Tokyo time". What is wrong, and what should they write?
Show answer
replace keeps the wall-clock fields and swaps the zone label, so 09:00 in New York becomes 09:00 in Tokyo, which is a different instant, 14 hours earlier in real time. They should write dt.astimezone(ZoneInfo("Asia/Tokyo")), which keeps the instant and recomputes the wall-clock fields.