Dates and times · 4 / 15 Theory

The calendar module and weekdays

Many date problems need calendar data: how many days a month has, whether a year is a leap year, which day of the week a date falls on. The calendar module and the weekday() method solve it without you having to memorize rules.

Days of the week

weekday() gives a number from 0 (Monday) to 6 (Sunday). isoweekday() goes from 1 (Monday) to 7 (Sunday). To get the name, use your own list.

from datetime import date

DAYS = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]

f = date(2026, 11, 14)
print(f.weekday())               # 5
print(f.isoweekday())            # 6
print(DAYS[f.weekday()])         # Saturday
print(f.weekday() >= 5)          # True   (weekend)

Leap years

A year is a leap year if it is divisible by 4, except those divisible by 100 that are not divisible by 400. So 2024 and 2000 are; 1900 and 2100 are not. calendar.isleap applies the rule.

import calendar

print(calendar.isleap(2024))   # True
print(calendar.isleap(1900))   # False
print(calendar.isleap(2000))   # True
print(calendar.isleap(2100))   # False

def is_leap(y):
    return y % 4 == 0 and (y % 100 != 0 or y % 400 == 0)

print(is_leap(2024), is_leap(2100))   # True False

Days in a month

calendar.monthrange(year, month) returns a pair: the day of the week on which the month starts and the number of days it has.

import calendar

print(calendar.monthrange(2026, 2))   # (6, 28)   February 2026 starts on a Sunday and has 28 days
print(calendar.monthrange(2024, 2))   # (3, 29)
print(calendar.monthrange(2026, 4)[1])   # 30

Practise with Leap years and days in a month.

Last day of the month, first Monday...

import calendar
from datetime import date, timedelta

# last day of a month
year, month = 2026, 11
last = date(year, month, calendar.monthrange(year, month)[1])
print(last)   # 2026-11-30

# first Monday of the month
first = date(year, month, 1)
first_monday = first + timedelta(days=(7 - first.weekday()) % 7)
print(first_monday)   # 2026-11-02

Showing a month

import calendar

print(calendar.month(2026, 11))
#    November 2026
# Mo Tu We Th Fr Sa Su
#                    1
#  2  3  4  5  6  7  8
#  9 10 11 12 13 14 15
# 16 17 18 19 20 21 22
# 23 24 25 26 27 28 29
# 30

Age and birthdays

Computing the age is not just subtracting the years: you have to see whether the birthday has already happened this year. A comparison of (month, day) tuples solves it.

from datetime import date

def age(birth, today):
    completed = today.year - birth.year
    if (today.month, today.day) < (birth.month, birth.day):
        completed -= 1
    return completed

print(age(date(1990, 5, 20), date(2026, 11, 14)))   # 36
print(age(date(1990, 12, 1), date(2026, 11, 14)))   # 35

You practise it in Compute the age. Beware of 29 February: date(year + 1, 2, 29) fails in a non-leap year, so you have to decide what to do in that case (for example, celebrate on 28 February or 1 March).

Events in order

With a list of (datetime, name) tuples, sort() orders the events by date and, if the dates are equal, by name.

from datetime import datetime

events = [
    (datetime(2026, 3, 16, 10, 0), "meeting"),
    (datetime(2026, 3, 15, 21, 0), "dinner"),
    (datetime(2026, 3, 15, 9, 30), "breakfast"),
]
events.sort()
for moment, name in events:
    print(moment.strftime("%d/%m %H:%M"), "-", name)
# 15/03 09:30 - breakfast
# 15/03 21:00 - dinner
# 16/03 10:00 - meeting

Apply it in Event schedule.