Python Arena
Step 2 of 3

Comparing and calculating with dates

Datetimes can be compared and subtracted, and time can be added to them. The datetime type takes care of the calendar: month lengths, leap years, the turn of the year.

Comparing

The comparison operators work on datetimes. An earlier moment is smaller than a later one:

from datetime import datetime

deadline = datetime(2026, 12, 14, 9, 0)
submitted = datetime(2026, 12, 14, 8, 45)

if submitted <= deadline:
    print("Submitted in time")
else:
    print("Too late")
Output
Submitted in time

The difference: timedelta

Subtracting one datetime from another gives a timedelta: a length of time, rather than a moment. Its attribute days holds the whole days, and seconds holds the rest, less than a day, in seconds:

from datetime import datetime

start = datetime(2026, 9, 1)
end = datetime(2026, 12, 18, 12, 0)
difference = end - start
print(difference)
print(difference.days)
print(difference.seconds)
Output
108 days, 12:00:00
108
43200

A timedelta has no attribute for hours, and none for months or years either, because months and years don't all have the same length:

from datetime import datetime

difference = datetime(2026, 3, 1, 18, 0) - datetime(2026, 3, 1, 9, 0)
print(difference.hours)
Python stops with
Traceback (most recent call last):
  File "main.py", line 4, in <module>
    print(difference.hours)
          ^^^^^^^^^^^^^^^^
AttributeError: 'datetime.timedelta' object has no attribute 'hours'

Calculate hours from the seconds yourself: difference.seconds // 3600 is 9 here.

Adding time

To move a moment forward or back, add or subtract a timedelta. You make one with timedelta(...), giving the units you want as keyword arguments, like end= in print: days, weeks, hours, minutes. Import timedelta from the module too:

from datetime import datetime, timedelta

order = datetime(2026, 11, 27, 16, 0)
print("Delivery:", order + timedelta(days=3))
print("Return by:", order + timedelta(weeks=4))
print("Reminder:", order - timedelta(hours=20))
Output
Delivery: 2026-11-30 16:00:00
Return by: 2026-12-25 16:00:00
Reminder: 2026-11-26 20:00:00

Adding a plain number, as in order + 3, is a TypeError: Python can't know whether you mean days, hours or seconds.

Your task

Challenge 1 of 3

What does this program print? Type each line it prints.

Challenge 2

A library lends books for a given number of days. Write a function due_date(borrowed, days) that gets the day the book was borrowed, as a datetime, and the length of the loan in days, and returns the datetime when the book is due.

due_date(datetime(2026, 2, 20), 14) returns the datetime for March 6, 2026.

Challenge 3

A shop gives a warranty of 730 days from the day of purchase. Write a program that asks for the date of purchase and prints the last day of the warranty, and then either how many days are left or how many days ago it ended. The program calculates from the day stored in today in the starter code (so that its output is the same whatever the date really is). For example:

Purchase year: 2025
Purchase month: 3
Purchase day: 14
Warranty ends 14.3.2027
Days left: 150

When the warranty has ended, the last line is like The warranty ended 136 days ago. On the last day itself, there are 0 days left.