LoslegenKostenlos loslegen

Subtracting dates

Python date objects let us treat calendar dates as something similar to numbers: we can compare them, sort them, add, and even subtract them. This lets us do math with dates in a way that would be a pain to do by hand.

The 2007 Florida hurricane season was one of the busiest on record, with 8 hurricanes in one year. The first one hit on May 9th, 2007, and the last one hit on December 13th, 2007. How many days elapsed between the first and last hurricane in 2007?

Diese Übung ist Teil des Kurses

Working with Dates and Times in Python

Kurs anzeigen

Anleitung zur Übung

  • Import date from datetime.
  • Create a date object for May 9th, 2007, and assign it to the start variable.
  • Create a date object for December 13th, 2007, and assign it to the end variable.
  • Subtract start from end, to print the number of days in the resulting timedelta object.

Interaktive Übung

Versuche dich an dieser Übung, indem du diesen Beispielcode vervollständigst.

# Import date
from ____ import ____

# Create a date object for May 9th, 2007
start = date(____, ____, ____)

# Create a date object for December 13th, 2007
end = date(____, ____, ____)

# Subtract the two dates and print the number of days
print((____ - ____).____)
Code bearbeiten und ausführen