Timedelta and Arithmetic
Compute differences between dates.
What is a timedelta?
A timedelta represents a duration — the gap between two points in time. It is the result of subtracting one date or datetime from another.
Import it with from datetime import timedelta.
from datetime import timedelta
gap = timedelta(days=3)
print(gap)Building a duration
Create a timedelta with keyword arguments such as days, hours, minutes, and seconds. Python normalizes them into days, seconds, and microseconds.
from datetime import timedelta
d = timedelta(days=1, hours=6, minutes=30)
print(d)All lessons in this course
- date, time, datetime
- Timedelta and Arithmetic
- Formatting and Parsing
- Time Zones with zoneinfo