Learn datetime

Dates, times, durations and time zones in the standard library

13 lessons · 2 hours · 4 levels · covers datetime Python 3.13

The datetime module provides date, time, datetime and timedelta types for calendar arithmetic, formatting and parsing, and together with zoneinfo it handles real-world time zones and daylight saving time. Scheduling, logging, billing, analytics and every API that exchanges timestamps depend on getting it right. It rewards care: naive versus aware values and DST transitions are classic sources of bugs.

Before you start

datetime builds on these; the tree links to the exact lessons you need.

How deep do you want to go?

1. EssentialsRead and write simple code, do the everyday basics4 lessons · 32 min in total
2. PracticalUse it productively at work, with the usual idioms7 lessons · 1 hour in total
3. AdvancedThe less common features, performance and trade-offs10 lessons · 2 hours in total
4. ExpertInternals and the edge cases experts know13 lessons · 2 hours in total

The datetime tree

datetimedate, time and datetimedate, time anddatetimeDurations with timedeltaDurations withtimedeltaFormatting with strftime and isoformatFormatting withstrftime and isofor…Parsing with fromisoformat and strptimeParsing withfromisoformat and…Naive vs aware datetimesNaive vs awaredatetimesReal time zones with zoneinfoReal time zones withzoneinfoUnix timestamps and clocksUnix timestamps andclocksSpecial methods and protocolsSpecial methods andprotocolsInheritance, super() and compositionInheritance, super()and compositionNumbers and arithmeticNumbers andarithmetic
Essentials and Practical shown (7 of 13 lessons); the full list is below. Click a lesson to read it.

All 13 lessons, in order

Dates and times

  1. date, time and datetime · Essentials · 8 min

    Create and inspect date, time and datetime objects, and get the current date and time.

  2. Durations with timedelta · Essentials · 8 min

    Represent durations with timedelta, add them to dates and datetimes, and measure the difference between two moments.

Formatting and parsing

  1. Formatting with strftime and isoformat · Essentials · 8 min

    Turn dates into text with isoformat, strftime format codes and f-string format specs.

  2. Parsing with fromisoformat and strptime · Essentials · 8 min

    Turn strings into datetimes with fromisoformat for ISO 8601 and strptime for custom formats.

Time zones

  1. Naive vs aware datetimes · Practical · 10 min

    Understand the difference between naive and aware datetimes, and use UTC-aware values by default.

  2. Real time zones with zoneinfo · Practical · 10 min

    Use zoneinfo.ZoneInfo with IANA names like Europe/Berlin to handle daylight saving time correctly.

  3. Unix timestamps and clocks · Practical · 9 min

    Convert between datetimes and Unix timestamps safely, and pick the right clock for measuring time.

Real-world date logic

  1. DST transitions, gaps and folds · Advanced · 12 min

    Handle the hour that doesn't exist and the hour that happens twice, and know how arithmetic behaves across DST changes.

  2. Month arithmetic and calendars · Advanced · 11 min

    Add months safely, find month ends, iterate date ranges and work with ISO weeks using datetime and calendar.

  3. Storing and exchanging times · Advanced · 10 min

    Choose formats for databases, JSON and logs: UTC instants, ISO 8601 strings, and local time plus zone for future events.

Semantics and internals

  1. Equality, hashing and arithmetic semantics · Expert · 12 min

    Understand intra-zone versus inter-zone comparison and subtraction, why fold is ignored in some places, and the hash consequences.

  2. Implementing a tzinfo · Expert · 12 min

    See how the tzinfo protocol works by implementing utcoffset, dst, tzname and fromutc, and why ZoneInfo is built the way it is.

  3. Precision, ranges and leap seconds · Expert · 9 min

    Know datetime's resolution and range, what happens with leap seconds and nanoseconds, and how float timestamps lose precision.