Sub topic 2
Measuring and shifting time with timedelta
This chapter assumes you know the date, time, and datetime types from the previous chapter. It also assumes you are comfortable with basic Python arithmetic and exceptions.
A timedelta is a fixed length of time, such as "10 days" or "90 minutes". It is not a point on the calendar. You use it to measure the gap between two points and to move a point forward or backward.
Creating a timedelta
All arguments are keywords and all are optional: weeks, days, hours, minutes, seconds, milliseconds, and microseconds. Values can be negative or fractional.
from datetime import timedelta
d = timedelta(weeks=1, days=2, hours=3, minutes=30)
print(d)
print(d.days, d.seconds, d.microseconds)
print(d.total_seconds())This prints 9 days, 3:30:00, then 9 12600 0, then 790200.0.
Python stores only three fields internally: days, seconds (0 to 86399), and microseconds. Every input is normalized into those three. Weeks become 7 days, and hours and minutes fold into seconds.
Months and years are not supported
timedelta(months=1) raises a TypeError. A month has 28 to 31 days and a year has 365 or 366, so neither is a fixed length. If you need "the same day next month", compute it yourself or use a third-party library such as python-dateutil.
Measuring a gap
Subtracting two date objects or two datetime objects returns a timedelta.
from datetime import date, datetime
print(date(2026, 12, 25) - date(2026, 11, 26))
start = datetime(2026, 11, 26, 9, 0)
end = datetime(2026, 11, 26, 11, 7)
print(end - start)The output is 29 days, 0:00:00 and 2:07:00.
Shifting a point in time
Adding or subtracting a timedelta moves a date or datetime.
from datetime import datetime, timedelta
deadline = datetime(2026, 11, 26, 17, 0)
print(deadline + timedelta(days=3))
print(deadline - timedelta(hours=36))This prints 2026-11-29 17:00:00 and 2026-11-25 05:00:00.
A plain date has no time of day, so only the days part of the timedelta is used. The hours and smaller units are ignored.
Arithmetic on durations
Timedeltas support the operators you would expect:
| Expression | Result type | Meaning |
|---|---|---|
| `td + td`, `td - td` | `timedelta` | Combine or compare lengths |
| `td * 3`, `td / 2`, `td // 2` | `timedelta` | Scale a length |
| `td / td` | `float` | How many times one fits in the other |
| `-td`, `abs(td)` | `timedelta` | Flip or drop the sign |
| `td < td` | `bool` | Compare lengths |
from datetime import timedelta
shift = timedelta(hours=8)
print(shift * 5)
print(timedelta(days=1) / shift)
print(abs(timedelta(minutes=-45)))The output is 1 day, 16:00:00, 3.0, and 0:45:00.
Two common errors
Using .seconds as the total
The seconds attribute is only the leftover part after whole days. For a total, call total_seconds().
from datetime import timedelta
d = timedelta(days=1, seconds=5)
print(d.seconds)
print(d.total_seconds())This prints 5 and 86405.0.
Surprising negative values
Normalization keeps seconds non-negative, so the sign is carried by days. timedelta(hours=-1) has days == -1 and seconds == 82800, and it prints as -1 day, 23:00:00. Use total_seconds() or abs() when you only care about magnitude.
Putting it together
A typical use is checking whether something has expired.
from datetime import datetime, timedelta
issued = datetime(2026, 11, 26, 9, 0)
lifetime = timedelta(hours=2)
now = datetime(2026, 11, 26, 11, 7)
remaining = issued + lifetime - now
print(remaining)
print(remaining < timedelta(0))This prints -1 day, 23:53:00 and True. The token expired 7 minutes ago. Comparing against timedelta(0) is the clearest way to test the sign of a duration.
Exercise
A job starts at datetime(2026, 11, 26, 22, 30) and runs for timedelta(hours=3, minutes=45). What is its end time, and what does (end - start).total_seconds() return?
Show answer
The end time is datetime(2026, 11, 27, 2, 15), printed as 2026-11-27 02:15:00. The duration is 3 hours 45 minutes, which is 13500.0 seconds.
Exercise
For d = timedelta(hours=-2), what are d.days and d.seconds, and which method gives the length as a single signed number?
Show answer
d.days is -1 and d.seconds is 79200, because Python normalizes so that seconds is never negative. d.total_seconds() returns -7200.0.