A paper calendar hangs beside the launch screen, and Rocket is counting the days to launch with his finger. He loses count at the end of June and starts again.
"How many days does June have again?" he asks. "Thirty? Thirty-one? Counting squares is not engineering."
Raven taps the calendar. "What do you notice?" she asks. "Months are different lengths, and some years have an extra day. That is a lot of rules to remember."
Nova projects two dates with a glowing gap between them. "Would you like a hint?" she asks. "The datetime module already knows every calendar rule. Subtract one date from another and it tells you the gap."
Rocket puts his finger down and imports date.
from datetime import date brings in the date type. date(2026, 7, 20) makes a date: year, then month, then day.
Subtracting two dates gives a timedelta, which is a length of time. Its .days part is the number of days between them. Adding a timedelta to a date moves it forward.
# dates.py
from datetime import date, timedelta
launch = date(2026, 7, 20)
check = date(2026, 6, 1)
gap = launch - check
print(gap.days, "days from check to launch")
print(launch.isoformat())
print(launch.strftime("%A %d %B %Y"))
print(check + timedelta(days=100)) 49 days from check to launch 2026-07-20 Monday 20 July 2026 2026-09-09
| Code | Gives | Notes |
|---|---|---|
| launch.isoformat() | 2026-07-20 | year-month-day, good for saving |
| launch.strftime("%A") | Monday | %A is the day of the week |
| launch.strftime("%d %B") | 20 July | %d is the day, %B the month name |
| launch.year | 2026 | also .month and .day |
date.today() gives today's date from the computer's clock. Its value depends on the day you run it, so its output changes from day to day.
When you test a program, a fixed date like date(2026, 6, 1) gives the same answer every run. Switch to date.today() once it works.
Adding days with timedelta can cross into a new month, and datetime handles it. Subtracting dates also knows how long each month is.
# calendar_math.py
from datetime import date, timedelta
start = date(2026, 1, 30)
print(start + timedelta(days=3))
print((date(2026, 3, 1) - date(2026, 2, 1)).days)
print(start.month, start.day)
print(start.strftime("%A")) 2026-02-02 28 1 30 Friday
| Statement | True or false? |
|---|---|
| date(2026, 2, 30) is a real date. | ? |
| Subtracting two dates gives a timedelta. | ? |
| .days gives the number of days in a timedelta. | ? |
| %B in strftime gives the month name. | ? |
You let Python handle the calendar rules. Tomorrow you will build a days until launch program.