Sub topic 10
Dates, times, datetimes, and timedeltas
This chapter assumes you know basic Python syntax, can use from ... import ..., and can run a script or the interactive interpreter. It introduces the four core types in the datetime module. The next chapters cover arithmetic, formatting, and time zones in more depth.
Four types, four jobs
The datetime module separates a point on the calendar from a length of time.
| Type | Represents | Example |
|---|---|---|
| `date` | A calendar day, with no time of day | 26 November 2026 |
| `time` | A time of day, with no date | 14:30:15 |
| `datetime` | A date and a time together | 26 November 2026, 14:30 |
| `timedelta` | A duration, not a point in time | 2 days and 3.5 hours |
Creating date, time, and datetime objects
Each constructor takes integers, largest unit first. Values outside the valid range raise ValueError, for example month 13 or day 31 in April.
from datetime import date, time, datetime
d = date(2026, 11, 26)
print(d.year, d.month, d.day) # 2026 11 26
print(d.weekday()) # 3 (Monday is 0)
print(d.isoweekday()) # 4 (Monday is 1)
t = time(14, 30, 15)
print(t.hour, t.minute, t.second) # 14 30 15
dt = datetime(2026, 11, 26, 14, 30)
print(dt.date()) # 2026-11-26
print(dt.time()) # 14:30:00
print(datetime.combine(d, t)) # 2026-11-26 14:30:15To get the current moment, use date.today() or datetime.now(). Their output depends on when you run them, so they are awkward to test. Pass the value in as a parameter when you can.
Instances are immutable. To change one field, call replace, which returns a new object:
from datetime import date
d = date(2026, 11, 26)
print(d.replace(day=1)) # 2026-11-01
print(d) # 2026-11-26 (unchanged)Building a timedelta
A timedelta accepts days, seconds, microseconds, milliseconds, minutes, hours, and weeks. There are no months or years arguments, because their lengths vary.
from datetime import timedelta
delta = timedelta(days=2, hours=3, minutes=30)
print(delta) # 2 days, 3:30:00
print(delta.days) # 2
print(delta.seconds) # 12600
print(delta.total_seconds()) # 185400.0Python stores a timedelta as three normalized fields: days, seconds (0 to 86399), and microseconds. So .seconds is only the leftover part after whole days, not the full length. Use total_seconds() when you want the whole duration.
from datetime import timedelta
back = timedelta(hours=-1)
print(back.days, back.seconds) # -1 82800
print(back.total_seconds()) # -3600.0A negative duration stores its sign in days. That makes .days and .seconds look odd for negative values, but total_seconds() stays correct.
Common mistakes
- Treating
delta.secondsas the full duration. Usedelta.total_seconds(). - Passing arguments in the wrong order, such as
date(26, 11, 2026). The order is year, month, day. - Calling
datetime.now()inside logic you want to test. Accept adatetimeargument instead. - Expecting
weekday()to start on Sunday. It starts on Monday as 0.
Exercise
What do timedelta(days=1, hours=-2).days, .seconds, and .total_seconds() return?
Show answer
The duration is 22 hours. Python stores it as 0 days and 79200 seconds, so .days is 0, .seconds is 79200, and .total_seconds() is 79200.0.
Exercise
Starting from d = date(2026, 11, 26), how do you get the first day of that month as a new date, and does d change?
Show answer
Use d.replace(day=1), which returns date(2026, 11, 1). d does not change, because date objects are immutable.