Python Basics: Working with Months and Weekdays Using calendar
For a monthly checklist, you might need the number of days, a grid of weeks, or every Friday. Python’s standard-library calendar module handles these small date-planning tasks.
Before you start
These independent examples were tested on Linux with Bash, Python 3.12.14 and Python 3.13.5. No packages or environment variables are required. The commands below use python3; use python3.13 instead to run them with that interpreter.
In one Bash session, create a fresh working directory:
start_dir="$PWD"
work_dir="$(mktemp -d)" || exit 1
cd -- "$work_dir" || exit 1Save each source block below in this directory using the stated filename. Avoid calendar.py, which would shadow the standard-library module. The programs only print results; the files you save are the only files to clean up.
Check February’s length
Save as demo_calendar_1.py:
import calendar
for year in (1900, 2000, 2024, 2025):
first_weekday, days = calendar.monthrange(year, 2)
print(f"{year}: leap={calendar.isleap(year)}, February days={days}")
try:
calendar.monthrange(2026, 13)
except calendar.IllegalMonthError:
print("Invalid month: use 1 through 12")Run it:
python3 demo_calendar_1.pyOutput:
1900: leap=False, February days=28
2000: leap=True, February days=29
2024: leap=True, February days=29
2025: leap=False, February days=28
Invalid month: use 1 through 12monthrange() returns the first day’s weekday and the month’s length. Weekdays use 0 for Monday through 6 for Sunday. This example only needs the length. isleap() handles century exceptions too: 1900 and 2000 differ. The invalid-month call is intentional and caught, so the script finishes normally.
Build a month as full weeks
Save as demo_calendar_2.py:
import calendar
planner = calendar.Calendar(firstweekday=calendar.MONDAY)
weeks = planner.monthdayscalendar(2026, 10)
print("Columns: Mon Tue Wed Thu Fri Sat Sun")
for week in weeks:
print(week)Run it:
python3 demo_calendar_2.pyOutput:
Columns: Mon Tue Wed Thu Fri Sat Sun
[0, 0, 0, 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, 31, 0]Each row contains seven positions. A zero is padding outside October, not a date. The Calendar instance keeps its own first-weekday setting. This grid could supply cells for a monthly checklist.
List Fridays for a recurring task
Save as demo_calendar_3.py:
import calendar
year, month = 2026, 10
planner = calendar.Calendar(firstweekday=calendar.SUNDAY)
for day in planner.itermonthdates(year, month):
if day.month == month and day.weekday() == calendar.FRIDAY:
print(day.isoformat())Run it:
python3 demo_calendar_3.pyOutput:
2026-10-02
2026-10-09
2026-10-16
2026-10-23
2026-10-30itermonthdates() yields date objects, including neighboring dates needed to fill complete weeks. The month check excludes those neighbors. A Sunday-first grid changes column order, not weekday numbers.
This lists candidate dates only. It neither checks holidays nor schedules a reminder.
Clean up
After running the examples, remove their files and the empty working directory in the same Bash session:
rm -- demo_calendar_1.py demo_calendar_2.py demo_calendar_3.py
cd -- "$start_dir" || exit 1
rmdir -- "$work_dir"
unset start_dir work_dirReferences
Acknowledgements
This post was created with assistance from AI.