Python's calendar module provides calendar-related functions: printing calendars, finding weekdays, counting days in months, and more.

Basic Calendar Output

import calendar
 
# Print a month
print(calendar.month(2026, 3))
#      March 2026
# Mo Tu We Th Fr Sa Su
#                    1
#  2  3  4  5  6  7  8
# ...
 
# Print full year
print(calendar.calendar(2026))

Month and Year Data

import calendar
 
# Days in a month
print(calendar.monthrange(2026, 2))  # (6, 28)
# (weekday of first day, number of days)
# 6 = Sunday, 28 days in February
 
# Is it a leap year?
print(calendar.isleap(2026))  # False
print(calendar.isleap(2024))  # True
 
# Leap days between years
print(calendar.leapdays(2000, 2026))  # 7

Weekday Functions

import calendar
 
# Weekday of a date (0=Monday, 6=Sunday)
print(calendar.weekday(2026, 3, 21))  # 5 (Saturday)
 
# Weekday names
print(calendar.day_name[0])   # Monday
print(calendar.day_abbr[0])   # Mon
 
# Month names
print(calendar.month_name[3])  # March
print(calendar.month_abbr[3])  # Mar

Iterating Over Months

import calendar
 
# Iterate days in a month
for week in calendar.monthcalendar(2026, 3):
    print(week)
# [0, 0, 0, 0, 0, 0, 1]  <- First week
# [2, 3, 4, 5, 6, 7, 8]
# ...
# 0 means day is outside this month
 
# Get all dates in month as (weekday, day) pairs
for day in calendar.Calendar().itermonthdays2(2026, 3):
    print(day)
# (6, 0)  <- Sunday, 0 = padding
# (0, 2)  <- Monday, March 2

Calendar Classes

import calendar
 
# Default: Monday first
cal = calendar.Calendar()
 
# Sunday first
cal = calendar.Calendar(firstweekday=6)
 
# Iterate weeks
for week in cal.monthdayscalendar(2026, 3):
    print(week)

HTML Calendar

import calendar
 
html_cal = calendar.HTMLCalendar()
 
# Month as HTML
html = html_cal.formatmonth(2026, 3)
print(html)
 
# Year as HTML
html = html_cal.formatyear(2026)
 
# With CSS classes
html_cal = calendar.HTMLCalendar()
html_cal.cssclasses = ["mon", "tue", "wed", "thu", "fri", "sat", "sun"]

Text Calendar

import calendar
 
text_cal = calendar.TextCalendar()
 
# Customized output
text_cal = calendar.TextCalendar(firstweekday=6)  # Sunday first
text_cal.prmonth(2026, 3)
 
# Get as string
month_str = text_cal.formatmonth(2026, 3)
year_str = text_cal.formatyear(2026)

Setting First Weekday

import calendar
 
# Global setting
calendar.setfirstweekday(calendar.SUNDAY)  # 6
print(calendar.firstweekday())  # 6
 
# Constants
print(calendar.MONDAY)    # 0
print(calendar.SUNDAY)    # 6

Practical Examples

Find All Mondays

import calendar
 
def mondays_in_month(year, month):
    cal = calendar.Calendar()
    return [
        day for day in cal.itermonthdays(year, month)
        if day != 0 and calendar.weekday(year, month, day) == 0
    ]
 
print(mondays_in_month(2026, 3))  # [2, 9, 16, 23, 30]

Business Days Count

import calendar
 
def business_days(year, month):
    """Count weekdays (Mon-Fri) in a month."""
    count = 0
    for day in range(1, calendar.monthrange(year, month)[1] + 1):
        if calendar.weekday(year, month, day) < 5:
            count += 1
    return count
 
print(business_days(2026, 3))  # 22

Last Day of Month

import calendar
 
def last_day_of_month(year, month):
    return calendar.monthrange(year, month)[1]
 
print(last_day_of_month(2026, 2))  # 28
print(last_day_of_month(2024, 2))  # 29 (leap year)

Find Nth Weekday

import calendar
 
def nth_weekday(year, month, weekday, n):
    """Find the nth occurrence of weekday in month.
    weekday: 0=Mon, 6=Sun
    n: 1 for first, 2 for second, etc.
    """
    cal = calendar.Calendar()
    days = [
        day for day in cal.itermonthdays(year, month)
        if day != 0 and calendar.weekday(year, month, day) == weekday
    ]
    if n <= len(days):
        return days[n - 1]
    return None
 
# Third Friday of March 2026
print(nth_weekday(2026, 3, 4, 3))  # 20

Generate Year Summary

import calendar
 
def year_summary(year):
    for month in range(1, 13):
        first_weekday, num_days = calendar.monthrange(year, month)
        print(f"{calendar.month_name[month]}: {num_days} days, "
              f"starts on {calendar.day_name[first_weekday]}")
 
year_summary(2026)

Localization

import calendar
import locale
 
# Set locale for month/day names
locale.setlocale(locale.LC_ALL, 'de_DE.UTF-8')
 
cal = calendar.LocaleTextCalendar(locale='de_DE.UTF-8')
print(cal.formatmonth(2026, 3))
# Uses German day/month names

Summary

The calendar module handles calendar calculations:

  • monthrange() for days in month
  • weekday() for day of week
  • isleap() for leap year check
  • Calendar class for iteration
  • TextCalendar/HTMLCalendar for output

Useful for scheduling, date pickers, and reporting—anything that needs calendar math.

React to this post: