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.

Python datetime: Dates, Times and Time Zones: date, Calendar day; datetime, Date and clock time; zoneinfo, Named time-zone rules.

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.