calendar

Updated March 13, 2026 · Modules
stdlib dates calendar time

The calendar module provides functions and classes for working with calendars and dates. It allows you to print plain-text calendars, generate HTML calendars, and perform date-related calculations like finding the day of the week for a given date.

Syntax

import calendar

No parentheses needed when importing the module itself.

Functions

The calendar module provides several utility functions:

calendar.month()

Returns a month’s calendar as multi-line string.

print(calendar.month(2026, 3))

calendar.prcal()

Prints a full year’s calendar directly.

calendar.prcal(2026)  # Prints the entire 2026 calendar to stdout

calendar.weekday()

Returns the day of the week for a given date. Monday is 0 and Sunday is 6.

calendar.weekday(2026, 3, 8)  # March 8, 2026
# 6 (Sunday)

Parameters

ParameterTypeDefaultDescription
yearintThe year (e.g., 2026)
monthintThe month (1-12)
dayintThe day of the month (1-31)

Return Value

Returns an integer: 0 (Monday) through 6 (Sunday).

calendar.monthrange()

Returns a tuple of the weekday of the first day of the month and number of days in that month.

calendar.monthrange(2026, 3)
# (6, 31)  # Sunday (6) is the first day, 31 days in March

Parameters

ParameterTypeDefaultDescription
yearintThe year (e.g., 2026)
monthintThe month (1-12)

Return Value

Returns a tuple (weekday, days) where weekday is 0-6 and days is the number of days in the month.

calendar.isleap()

Determines whether a given year is a leap year.

calendar.isleap(2024)  # True
calendar.isleap(2026)  # False

Parameters

ParameterTypeDefaultDescription
yearintThe year to check

Return Value

Returns True if the year is a leap year, False otherwise.

calendar.leapdays()

Returns the number of leap years between two years (exclusive of the end year).

calendar.leapdays(2020, 2030)  # 3 leap years (2020, 2024, 2028)

Parameters

ParameterTypeDefaultDescription
year1intThe first year (inclusive)
year2intThe second year (exclusive)

Return Value

Returns an integer representing the count of leap years between the two years.

Leap Year Functions

calendar.isleap(2024)  # True - 2024 is a leap year
calendar.isleap(2026)  # False

# Count leap years in a range
calendar.leapdays(2020, 2030)  # 3 leap years (2020, 2024, 2028)

Calendar Classes

The module provides calendar classes for more control:

TextCalendar

c = calendar.TextCalendar(calendar.MONDAY)
c.prmonth(2026, 3)

HTMLCalendar

hc = calendar.HTMLCalendar()
html = hc.formatmonth(2026, 3)
print(html)  # Returns HTML table

Examples

Find all Fridays the 13th in a year

import calendar

year = 2026
fridays_13th = []
for month in range(1, 13):
    if calendar.weekday(year, month, 13) == 4:  # Friday is 4
        fridays_13th.append(calendar.month_name[month])
        
print(fridays_13th)
# ['February', 'March', 'November']
import calendar

c = calendar.TextCalendar()
# Print calendar with custom column width
print(c.formatmonth(2026, 3).center(30))

Common Patterns

Checking if a year is a leap year

import calendar

def is_leap_year(year):
    return calendar.isleap(year)

print(is_leap_year(2024))  # True
print(is_leap_year(2025))  # False

Finding the first Monday of a month

import calendar

def first_monday(year, month):
    first_day, num_days = calendar.monthrange(year, month)
    # first_day is 0=Monday, 6=Sunday
    if first_day == 0:
        return 1
    return 8 - first_day

print(f"First Monday of March 2026: {first_monday(2026, 3)}")
# First Monday of March 2026: 2

Iterating over days in a month

import calendar
from datetime import date

year, month = 2026, 3
for day in range(1, calendar.monthrange(year, month)[1] + 1):
    d = date(year, month, day)
    print(f"{d}: {calendar.day_name[d.weekday()]}")

Errors

No exceptions raised by the functions in this module. All functions validate input gracefully and return appropriate values.

See Also