0Pricing
Python Academy · Lesson

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

  1. date, time, datetime
  2. Timedelta and Arithmetic
  3. Formatting and Parsing
  4. Time Zones with zoneinfo
← Back to Python Academy