← Back to course
Code Crew G6+ / Week 07 / Thursday
4/6
Week 07 Β· The standard library

Thursday

Dates with datetime
// random and datetime
⏱ about 30 min

Thursday: Dates with datetime

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.

date values

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
CodeGivesNotes
launch.isoformat()2026-07-20year-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.year2026also .month and .day
From date(2026, 6, 1) to date(2026, 6, 15), how many days is (later - earlier).days?
What does date(2026, 3, 9).isoformat() give?

Today's date

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.

DATES
  • Read the question.
  • Tap your answer.
Which line makes the date 4 October 2026?
Why test with a fixed date instead of date.today()?
At your computer
1. Type dates.py, save it and run it. Check the four lines.
2. Change launch to date(2026, 12, 25). Predict whether the gap is bigger, then run it.
3. In the shell, type from datetime import date and press Enter.
4. Then type date.today() and press Enter to see the computer's date.

Practice: more calendar math

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
READ CALENDAR_MATH.PY
  • Read the question.
  • Tap your answer.
Why does the first line show February?
What does the 28 tell you?
What does start.month give?
What is date(2026, 6, 1) + timedelta(days=30)? (year-month-day)
What is (date(2026, 6, 20) - date(2026, 6, 5)).days?
What does date(2026, 6, 1).strftime("%A") give? It is a ____
StatementTrue 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.?
WHY THIS EXERCISEdatetime refuses impossible dates, and subtracting dates gives a timedelta with a .days part.

You let Python handle the calendar rules. Tomorrow you will build a days until launch program.

← Wednesday