← All lessons

datetime: dates

Sign in · Sign up

Lesson 7.8

The datetime module works with dates:

from datetime import date

today = date.today()
summer = date(today.year, 6, 1)

print((summer - today).days)

Subtracting two dates gives a timedelta, and it has .days — the number of days. Months in date(...) are numbered from 1, so June is 6.

The check sets the dates start and end as lists [year, month, day]. Build date from them and print how many days are between them, with .days.

Hint

date(year, month, day) takes three numbers, not one string.

The lists start and end are [year, month, day], take the items by index. The difference of two dates gives a timedelta, and it has .days.