Python datetime: Dates, Times and Time Zones
Python's datetime tools model dates, clock readings and durations. Choosing the correct type helps prevent accidental time-zone conversions.
Date versus datetime
A date represents a calendar day with year, month and day. It is suitable for a birthday, holiday or invoice date when there is no intended clock time. A datetime adds hour, minute, second and microsecond components. It may be naive or aware of a time-zone relationship.
A date alone is not midnight in every region. Converting a date to an instant requires choosing a time and a zone. For the conceptual difference, see What Is a Calendar Date? and wall-clock time.
Naive and aware datetimes
A naive datetime has no usable time-zone information. It can represent a local clock reading whose location is not yet specified, but it does not by itself identify an unambiguous instant. An aware datetime has time-zone information through its tzinfo and a defined UTC offset for the value.
When a record represents an event that happened at a precise moment, an aware UTC datetime is often a clear interchange choice. When it represents a future local appointment, also preserve the intended named region and local date and time. Avoid mixing naive and aware values without an explicit conversion policy.
Using zoneinfo for regional rules
Python's standard-library zoneinfo module can use IANA time-zone data to represent regions such as America/New_York. Regional rules include offset changes over time. The zone data comes from the system or an installed time-zone data package, so availability and version depend on the environment.
Local clock transitions need care. A repeated local time can map to two instants; Python's fold attribute distinguishes the earlier and later occurrence for supported zones. A skipped local time also needs an explicit validation policy. Merely attaching a zone does not automatically reject every nonexistent local time. See the Python zoneinfo documentation.
Timedelta and calendar arithmetic
timedelta describes a duration using days, seconds and microseconds. It is useful for fixed intervals, but it does not represent a calendar month because months have different lengths. Adding 30 days is not necessarily the same as moving to the same date next month.
For month or year calculations, choose a calendar-aware operation and define what should happen when the destination month lacks the original day. Our date calculator documents its own end-of-month policy. If elapsed time across a clock transition matters, convert aware instants to UTC before comparing them and verify the library's arithmetic rules.
Interchange and testing
For data exchange, use an explicit ISO-style date-time with a UTC marker or offset when the value is an instant. Preserve date-only values as dates, and include a named zone separately when it carries future scheduling intent. Confirm whether an API expects Unix seconds or milliseconds before conversion.
Test ordinary dates, leap days, midnight around different offsets and both sides of daylight-saving transitions. The official datetime documentation defines supported types and operations. For more background, read ISO 8601 and daylight saving time.
Converting between zones, step by step
from datetime import datetime, timezone
from zoneinfo import ZoneInfo
meeting = datetime(2026, 3, 10, 9, 0, tzinfo=ZoneInfo("America/New_York"))
meeting.astimezone(ZoneInfo("Europe/London"))
# 2026-03-10 13:00:00+00:00
meeting.astimezone(timezone.utc).isoformat()
# '2026-03-10T13:00:00+00:00'
On 10 March 2026 New York is already on daylight time (UTC−4) but London is not, so the gap is four hours. zoneinfo takes that from the IANA rules. On Windows, or in slim containers without system tz data, install the tzdata package so ZoneInfo can find the rules.
Gotchas worth a test
datetime.utcnow()returns a naive value and is deprecated since Python 3.12. Usedatetime.now(timezone.utc).- Comparing a naive and an aware datetime with
<raisesTypeError. Equality just returnsFalse, which can hide the mix-up. - Adding a
timedeltato an aware datetime does wall-clock arithmetic within its zone. Across a clock change,+ timedelta(hours=24)lands on the same local time the next day, even though 23 or 25 real hours passed. Convert to UTC for elapsed time. - On 1 November 2026, 01:30 happens twice in New York.
fold=0selects the first one (EDT, UTC−4) andfold=1the second (EST, UTC−5). datetime.fromisoformat()accepts a trailingZfrom Python 3.11. Older versions need+00:00.
Common questions
Should I still use pytz?
For new code on Python 3.9 or later, zoneinfo is the standard choice. pytz needs its own localize() call, and passing a pytz zone straight to the datetime constructor gives a historical offset that is usually wrong.
What should go into the database?
Usually an aware UTC datetime for instants, a date for date-only values, and a local datetime plus a zone name for future appointments. Convert to the reader's zone only for display.