Python datetime and zoneinfo: Parse, Format and Convert Time Zones Without pytz

Dates look simple until a meeting scheduled for 9:00 in Paris shows up at 3:00 in New York, or a report adds 24 hours to a Saturday in March and lands on 13:00 instead of noon. Python's standard library has everything needed to get this right: datetime for the values, and, since Python 3.9, zoneinfo for real time zones with their daylight saving rules. This guide covers parsing, formatting, converting and the traps that produce wrong times without raising any error.
All examples were run with Python 3.13 on Linux. Nothing needs installing on Linux or macOS. On Windows, which ships no time zone database that Python can read, run pip install tzdata once, ideally inside a virtual environment; zoneinfo picks it up automatically.
Parse a string into a datetime
datetime.strptime() reads a string according to a format you describe with codes such as %d (day), %m (month), %Y (four-digit year), %H (hour, 24-hour clock) and %M (minutes):
from datetime import datetime
d = datetime.strptime("16/11/2026 14:30", "%d/%m/%Y %H:%M")
print(repr(d)) # datetime.datetime(2026, 11, 16, 14, 30)
The format must describe the whole string. If the input has seconds you did not declare, you get ValueError: unconverted data remains: :00; if a field does not match, ValueError: time data '...' does not match format '...'. The fix is always to adjust the format, the same way you clean input before converting it in our guide to ValueError: could not convert string to float.
For ISO 8601 strings, the format used by APIs, databases and JSON, skip strptime and use fromisoformat(). Since Python 3.11 it accepts almost every ISO 8601 variant, including the trailing Z for UTC:
datetime.fromisoformat("2026-11-16T14:30:00+01:00")
# datetime.datetime(2026, 11, 16, 14, 30, tzinfo=datetime.timezone(datetime.timedelta(seconds=3600)))
datetime.fromisoformat("2026-11-16T13:30:00Z")
# datetime.datetime(2026, 11, 16, 13, 30, tzinfo=datetime.timezone.utc)
On Python 3.10 and earlier, fromisoformat() rejects the Z; replace it with +00:00 first or upgrade.
Format a datetime as text
strftime() is the reverse operation and uses the same codes. isoformat() produces the ISO 8601 form, which is the one to store or send to another program:
d.strftime("%A %d %B %Y, %H:%M") # 'Monday 16 November 2026, 14:30'
d.isoformat() # '2026-11-16T14:30:00'
Day and month names from %A and %B follow the process locale, so the same code can print lundi on a French server. For text shown to users in several languages, a library such as Babel is more predictable than changing the locale. A datetime is not accepted by json.dumps() (TypeError: Object of type datetime is not JSON serializable): convert it with isoformat() before saving, as shown in reading and writing JSON files in Python, and parse it back with fromisoformat().
Naive and aware datetimes
This distinction causes most date bugs. A naive datetime has no time zone: datetime(2026, 11, 16, 14, 30) means "14:30 somewhere". An aware datetime carries a tzinfo and therefore designates one precise instant. Python refuses to compare the two:
naive < aware
# TypeError: can't compare offset-naive and offset-aware datetimes
A simple rule avoids the problem: make every datetime aware as soon as it enters your program, store and compute in UTC, and convert to a local zone only for display. For the current time, use datetime.now(timezone.utc). The older datetime.utcnow() returns a naive value that many libraries then mistake for local time; it has been deprecated since Python 3.12.
Attach a time zone with zoneinfo
A fixed offset such as +01:00 is not a time zone. Paris is at +01:00 in winter and +02:00 in summer, and the dates of the change are political decisions that have changed in the past. zoneinfo.ZoneInfo loads the full history of a zone from the IANA time zone database, using names such as Europe/Paris or America/New_York:
from datetime import datetime, timezone
from zoneinfo import ZoneInfo
paris = ZoneInfo("Europe/Paris")
meeting = datetime(2026, 11, 16, 14, 30, tzinfo=paris)
print(meeting) # 2026-11-16 14:30:00+01:00
print(meeting.tzname()) # CET
print(meeting.utcoffset()) # 1:00:00
To give a zone to a naive value that you know is local time, for instance a time read from a spreadsheet, use replace(tzinfo=...). It keeps the clock reading and only adds the zone:
naive = datetime(2026, 11, 16, 14, 30)
naive.replace(tzinfo=paris) # 2026-11-16 14:30:00+01:00
Always use region names, never abbreviations: CET, EST or IST are ambiguous or do not follow daylight saving time. The list of valid names is available with zoneinfo.available_timezones().
Convert between time zones
astimezone() keeps the instant and changes the clock reading:
meeting.astimezone(ZoneInfo("America/New_York")) # 2026-11-16 08:30:00-05:00
meeting.astimezone(ZoneInfo("Asia/Tokyo")) # 2026-11-16 22:30:00+09:00
meeting.astimezone(timezone.utc) # 2026-11-16 13:30:00+00:00
Remember the difference: replace(tzinfo=...) says "this clock reading was in that zone", astimezone(...) says "what would a clock in that zone show at this instant". Using replace to convert is a classic bug that shifts every time by the offset.
Daylight saving time traps
In the European Union, clocks go forward on the last Sunday of March and back on the last Sunday of October. Three consequences catch people out.
Arithmetic on aware datetimes follows the wall clock. Adding timedelta(days=1) to noon on Saturday 28 March 2026 in Paris gives noon on Sunday, even though only 23 hours have elapsed:
from datetime import timedelta
a = datetime(2026, 3, 28, 12, 0, tzinfo=paris)
b = a + timedelta(days=1)
print(b) # 2026-03-29 12:00:00+02:00
print(b - a) # 1 day, 0:00:00
print(b.astimezone(timezone.utc) - a.astimezone(timezone.utc)) # 23:00:00
That is usually what you want for calendar events, and usually not what you want for durations, billing or timeouts. For elapsed time, compute in UTC.
Some local times do not exist. On 29 March 2026, Paris jumps from 02:00 to 03:00. datetime(2026, 3, 29, 2, 30, tzinfo=paris) raises no error; it is simply given the winter offset, +01:00. If users type times into a form, check for this gap before saving.
Some local times happen twice. On 25 October 2026, 02:30 occurs once at +02:00 and again an hour later at +01:00. The fold attribute chooses between them: fold=0 (the default) is the first occurrence, fold=1 the second:
y = datetime(2026, 10, 25, 2, 30, tzinfo=paris)
y.utcoffset() # 2:00:00
y.replace(fold=1).utcoffset() # 1:00:00
Timestamps
A Unix timestamp counts seconds since 1 January 1970 UTC. Convert with an explicit zone in both directions, otherwise Python silently uses the machine's local time:
datetime.fromtimestamp(1_800_000_000, tz=timezone.utc)
# 2027-01-15 08:00:00+00:00
meeting.timestamp() # 1794835800.0
What about pytz and dateutil?
Older code and tutorials use pytz. Its time zones must be attached with tz.localize(dt); passing them to the datetime constructor gives an offset from the 19th century (for Paris, +00:09). zoneinfo has no such trap and works with the normal tzinfo= argument, so new code does not need pytz. python-dateutil remains useful for its flexible parser and for relative deltas such as "same day next month", which timedelta cannot express.
Summary
- Parse with
strptime()for custom formats andfromisoformat()for ISO 8601. - Make datetimes aware immediately; get the current time with
datetime.now(timezone.utc). - Use
ZoneInfo("Region/City"), never fixed offsets or abbreviations; on Windows, installtzdata. replace(tzinfo=)labels a clock reading;astimezone()converts an instant.- Compute durations in UTC, and check for missing or repeated local times around daylight saving changes.
- Store and exchange dates as ISO 8601 strings.
Sources
- Python documentation, datetime: Basic date and time types (format codes,
fromisoformat,fold, deprecation ofutcnow). - Python documentation, zoneinfo: IANA time zone support (data sources and the
tzdatapackage). - Python Enhancement Proposals, PEP 495: Local Time Disambiguation.
Pythonpathlib file paths
Join paths with /, split them into name, stem and suffix, find files with glob, and stop fighting backslashes on Windows.
PythonCould not convert string to float
Why float() raises "could not convert string to float": empty strings, commas, currency symbols, N/A values, and how to fix them in Python and pandas.
Datapandas groupby
Split, apply, combine: totals per group, named aggregation, transform, filter and the defaults that changed in pandas 3.