0Pricing
Python Academy · Lesson

Timedelta and Arithmetic

Compute differences between dates.

Timedelta and Arithmetic is a free Python Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Python Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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)

Subtracting two dates

Subtracting one date from another produces a timedelta. This is how you measure how far apart two days are.

from datetime import date
start = date(2026, 1, 1)
end = date(2026, 5, 30)
diff = end - start
print(diff)

Days between dates

The .days attribute of a timedelta gives the whole-day count as an integer — perfect for answering 'how many days until X?'.

from datetime import date
new_year = date(2027, 1, 1)
today = date(2026, 5, 30)
print('Days left:', (new_year - today).days)

Adding time to a date

Add a timedelta to a date or datetime to move forward in time. Subtract it to go backward.

from datetime import date, timedelta
today = date(2026, 5, 30)
in_two_weeks = today + timedelta(days=14)
print('In two weeks:', in_two_weeks)

Going back in time

Subtracting a timedelta shifts a date into the past. Here we find the date one week ago.

from datetime import date, timedelta
today = date(2026, 5, 30)
last_week = today - timedelta(weeks=1)
print('A week ago:', last_week)

Total seconds in a duration

The .total_seconds() method returns the entire duration expressed as a single floating-point number of seconds. Useful for precise time math.

from datetime import timedelta
d = timedelta(hours=2, minutes=30)
print('Total seconds:', d.total_seconds())

Datetime arithmetic

The same rules apply to datetime objects. Adding a duration shifts both the date and the clock time correctly, rolling over days when needed.

from datetime import datetime, timedelta
start = datetime(2026, 5, 30, 23, 0)
later = start + timedelta(hours=3)
print(later)

Multiplying durations

A timedelta can be multiplied by a number to scale it. This makes it easy to compute, say, five sprint cycles of two weeks each.

from datetime import timedelta
sprint = timedelta(weeks=2)
five_sprints = sprint * 5
print(five_sprints)

Comparing durations

Durations can be compared with each other using <, >, and ==. This lets you check whether one span of time is longer than another.

from datetime import timedelta
a = timedelta(hours=5)
b = timedelta(days=1)
print(a < b)
print(max(a, b))

Splitting days and seconds

A timedelta stores .days and .seconds separately. The .seconds attribute only holds the leftover seconds within the last day (0 to 86399), not the total.

from datetime import timedelta
d = timedelta(days=2, hours=3)
print('days:', d.days)
print('seconds:', d.seconds)

Quick Check

What does subtracting one date from another return?

Recap: Timedelta and Arithmetic

Key takeaways:

  • timedelta(days=, hours=, ...) builds a duration.
  • Subtracting two dates or datetimes returns a timedelta.
  • Add or subtract a timedelta to move dates forward or backward.
  • Read .days or call .total_seconds() for numeric values, and multiply durations to scale them.
from datetime import date, timedelta
today = date(2026, 5, 30)
print('In 100 days:', today + timedelta(days=100))

Frequently asked questions

Is the “Timedelta and Arithmetic” lesson free?

Yes — the full text of “Timedelta and Arithmetic” is free to read here on the web, and the Python Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Python Academy course, upgrade to CoddyKit PRO.

What will I learn in “Timedelta and Arithmetic”?

Compute differences between dates. You practise Python Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Python Academy?

No prior experience is required. Python Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Timedelta and Arithmetic” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Python Academy lesson?

Yes. Every Python Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

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