🎓 A1Learner
🔍
0 of 52 lessons completed

Python Dates in Real Programs

A date names one day on the calendar. Real programs use dates for due dates and gaps between days.

A date holds three numbers

date(2026, 11, 8) builds one day. The year, month, and day come back as numbers.

from datetime import date
start = date(2026, 11, 8)
print(start)
print(start.year)
print(start.month)

Add two weeks to a date

timedelta(days=14) is a span of fourteen days. Adding it moves the date forward.

from datetime import date, timedelta
start = date(2026, 11, 8)
due = start + timedelta(days=14)
print(due)

Count the days between dates

Subtracting dates gives a gap. The .days value holds the count.

from datetime import date
start = date(2026, 11, 8)
end = date(2026, 11, 22)
print((end - start).days)

Name the weekday

strftime("%A") turns a date into a weekday name.

from datetime import date
start = date(2026, 11, 8)
print(start.strftime("%A"))

Example

from datetime import date, timedelta
borrowed = date(2026, 11, 8)
today = date(2026, 11, 12)
due = borrowed + timedelta(days=14)
left = (due - today).days
print("Borrowed:", borrowed)
print("Due:", due)
print("Days left:", left)

Key points

  • date: a class that holds one calendar day
  • Use date(year, month, day) to build one date
  • timedelta: a span of time, like fourteen days
  • Use timedelta(days=n) to add days to a date
  • Subtract one date from another to get the gap
  • The gap has a .days value with the count
  • Use strftime("%A") to get the weekday name

In this example, the book was borrowed on 8 November 2026. Today is set to 12 November 2026. Adding fourteen days gives a due date of 22 November. Subtracting today from the due date leaves ten days. The three printouts show the borrowed date, the due date, and the days left.

Note

A date holds no clock time, while a datetime adds hours and minutes.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Python Playground

Exercise

A book is borrowed on 2026-11-08 for 14 days. What is the due date?