We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Comparing Dates

Sometimes you don't need to do date math; you just need to know which date comes first. Questions like:

  • Was this book returned before its due date?
  • Is this record inside the reporting window?
  • Which event happened earliest?

Luckily, compatible datetime values support normal comparison operators

Basic Comparisons

You can compare two datetime values with operators like <, >, <=, and >=:

returned_at = datetime(2026, 5, 15)
due_date = datetime(2026, 6, 1)

if returned_at < due_date:
    print("Book returned on time")

Which comes in handy whenever you want to filter a dataset by date.

Sorting Dates

You can also sort datetime values directly:

dates = [datetime(2026, 3, 1), datetime(2026, 1, 1), datetime(2026, 2, 1)]
dates.sort()
print(dates)
# [datetime.datetime(2026, 1, 1, 0, 0), datetime.datetime(2026, 2, 1, 0, 0), datetime.datetime(2026, 3, 1, 0, 0)]

Assignment

SnackStack's ops team needs to know which appliances are overdue for a firmware update. Policy says a device must be updated within a certain number of minutes after its firmware was installed.

Complete the needs_firmware_update function. It accepts:

  • installed_at: when the device's current firmware was installed
  • required_after_mins: the number of minutes after installation that an update becomes required
  • checked_at: the time of the compliance check