On this page
calendar
Prev: cProfile | Up: stdlib_module | Next: cmath
Rel: datetime, zoneinfo, collections, heapq, bisect
Ref: docs.python.org — calendar
Tags: #public #python3 #stdlib
Import
import calendar
Overview
Gregorian calendar calculations, weekday/month iterators, leap-year checks (isleap, monthrange, monthcalendar), and plain-text/HTML calendar rendering. Pair calendar with datetime when computing month lengths, billing-cycle boundaries, or weekly grid layouts.
Quick Start
import calendar
assert calendar.isleap(2024) and not calendar.isleap(2026)
first_weekday, num_days = calendar.monthrange(2026, 12)
assert num_days == 31
assert calendar.day_name[first_weekday] == "Tuesday"
Implementation Walkthrough
1. Iterating Dates in a Month (Calendar.itermonthdates)
Calendar(firstweekday=calendar.SUNDAY) yields full weeks of datetime.date objects (including leading/trailing padding days from adjacent months) for building calendar UIs:
import calendar
cal = calendar.Calendar(firstweekday=calendar.SUNDAY)
dec_days = [d for d in cal.itermonthdates(2026, 12) if d.month == 12]
assert len(dec_days) == 31 and dec_days[0].day == 1 and dec_days[-1].day == 31
2. Finding the Last Day of Any Month
import calendar
from datetime import date
def month_end(year: int, month: int) -> date:
_, last_day = calendar.monthrange(year, month)
return date(year, month, last_day)
assert month_end(2024, 2) == date(2024, 2, 29)
Gotchas & Boundaries
- Months are 1-indexed (
1..12), weekdays are 0-indexed (0=Monday .. 6=Sunday):calendar.month_name[0]is""somonth_name[1] == "January". - Default
firstweekdayis Monday (0): Passfirstweekday=calendar.SUNDAY(or6) for US-style Sunday-first calendars. calendar.timegm()converts a UTC time tuple to a Unix timestamp, unliketime.mktime()which assumes local time.
Common Functions
- calendar.calendar() —
calendar(theyear, w=2, l=1, c=6, m=3)— Returns a year's calendar as a multi-line string. - calendar.firstweekday() —
firstweekday()—calendar.firstweekday()function. - calendar.isleap() —
isleap(year)— Return True for leap years, False for non-leap years. - calendar.leapdays() —
leapdays(y1, y2)— Return number of leap years in range [y1, y2). - calendar.month() —
month(theyear, themonth, w=0, l=0)— Return a month's calendar string (multi-line). - calendar.monthcalendar() —
monthcalendar(year, month)— Return a matrix representing a month's calendar. - calendar.monthrange() —
monthrange(year, month)— Return weekday of first day of month (0-6 ~ Mon-Sun) and number of days (28-31) for year, month. - calendar.prcal() —
prcal(theyear, w=0, l=0, c=6, m=3)— Print a year's calendar. - calendar.prmonth() —
prmonth(theyear, themonth, w=0, l=0)— Print a month's calendar. - calendar.setfirstweekday() —
setfirstweekday(firstweekday)—calendar.setfirstweekday()function. - calendar.timegm() —
timegm(tuple)— Unrelated but handy function to calculate Unix timestamp from GMT. - calendar.weekday() —
weekday(year, month, day)— Return weekday (0-6 ~ Mon-Sun) for year, month (1-12), day (1-31). - calendar.weekheader() —
weekheader(width)— Return a header for a week.
Classes & Objects
- calendar.Calendar — Base calendar class. This class doesn't do any formatting. It simply provides data to subclasses. (Methods:
.firstweekday(),.getfirstweekday(),.itermonthdates(),.itermonthdays(),.itermonthdays2(),.itermonthdays3()) - calendar.Day —
calendar.Dayclass. (Methods:.as_integer_ratio(),.bit_count(),.bit_length(),.conjugate(),.from_bytes(),.is_integer()) - calendar.HTMLCalendar — This calendar returns complete HTML pages. (Methods:
.firstweekday(),.formatday(),.formatmonth(),.formatmonthname(),.formatweek(),.formatweekday()) - calendar.IllegalMonthError —
calendar.IllegalMonthErrorclass. (Methods:.add_note(),.with_traceback()) - calendar.IllegalWeekdayError —
calendar.IllegalWeekdayErrorclass. (Methods:.add_note(),.with_traceback()) - calendar.LocaleHTMLCalendar — This class can be passed a locale name in the constructor and will return month and weekday names in the specified locale. (Methods:
.firstweekday(),.formatday(),.formatmonth(),.formatmonthname(),.formatweek(),.formatweekday()) - calendar.LocaleTextCalendar — This class can be passed a locale name in the constructor and will return month and weekday names in the specified locale. (Methods:
.firstweekday(),.formatday(),.formatmonth(),.formatmonthname(),.formatweek(),.formatweekday()) - calendar.Month —
calendar.Monthclass. (Methods:.as_integer_ratio(),.bit_count(),.bit_length(),.conjugate(),.from_bytes(),.is_integer()) - calendar.TextCalendar — Subclass of Calendar that outputs a calendar as a simple plain text similar to the UNIX program cal. (Methods:
.firstweekday(),.formatday(),.formatmonth(),.formatmonthname(),.formatweek(),.formatweekday())
Constants & Attributes
calendar.APRIL(Month) =calendar.APRILcalendar.AUGUST(Month) =calendar.AUGUSTcalendar.DECEMBER(Month) =calendar.DECEMBERcalendar.FEBRUARY(Month) =calendar.FEBRUARYcalendar.FRIDAY(Day) =calendar.FRIDAYcalendar.JANUARY(Month) =calendar.JANUARYcalendar.JULY(Month) =calendar.JULYcalendar.JUNE(Month) =calendar.JUNEcalendar.MARCH(Month) =calendar.MARCHcalendar.MAY(Month) =calendar.MAYcalendar.MONDAY(Day) =calendar.MONDAYcalendar.NOVEMBER(Month) =calendar.NOVEMBERcalendar.OCTOBER(Month) =calendar.OCTOBERcalendar.SATURDAY(Day) =calendar.SATURDAYcalendar.SEPTEMBER(Month) =calendar.SEPTEMBER
Linked from
- tuts/python3/stdlib_module
- tuts/python3/stdlib_module/array
- tuts/python3/stdlib_module/bisect
- tuts/python3/stdlib_module/cmath
- tuts/python3/stdlib_module/collections
- tuts/python3/stdlib_module/copy
- tuts/python3/stdlib_module/cProfile
- tuts/python3/stdlib_module/datetime
- tuts/python3/stdlib_module/enum
- tuts/python3/stdlib_module/graphlib
- tuts/python3/stdlib_module/heapq
- tuts/python3/stdlib_module/pprint
- tuts/python3/stdlib_module/reprlib
- tuts/python3/stdlib_module/stdlib_module
- tuts/python3/stdlib_module/types
- tuts/python3/stdlib_module/weakref
- tuts/python3/stdlib_module/zoneinfo
Related notes
- tuts/python3/stdlib_module/bisect Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/cmath Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/collections Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/cProfile Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/datetime Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/heapq Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/stdlib_module Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/zoneinfo Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/array links to this page; shared tags: #stdlib