On this page
calendar.Calendar
Prev: calendar.calendar | Up: calendar | Next: calendar.Day
Rel: calendar.calendar, calendar.Day, calendar.firstweekday, calendar.HTMLCalendar
Ref: docs.python.org — calendar.Calendar
Tags: #public #python3 #stdlib #calendar
Import
import calendar
from calendar import Calendar
Signature & Overview
calendar.Calendar(firstweekday=0)
Base calendar class. This class doesn't do any formatting. It simply provides data to subclasses.
Methods & Attributes
Calendar.firstweekday(...)—Calendar.firstweekdaymethod/property.Calendar.getfirstweekday(self)—Calendar.getfirstweekdaymethod/property.Calendar.itermonthdates(self, year, month)— Return an iterator for one month. The iterator will yield datetime.date values and will always iterate through complete weeks, so it will yield dates outside the specified month.Calendar.itermonthdays(self, year, month)— Like itermonthdates(), but will yield day numbers. For days outside the specified month the day number is 0.Calendar.itermonthdays2(self, year, month)— Like itermonthdates(), but will yield (day number, weekday number) tuples. For days outside the specified month the day number is 0.Calendar.itermonthdays3(self, year, month)— Like itermonthdates(), but will yield (year, month, day) tuples. Can be used for dates outside of datetime.date range.Calendar.itermonthdays4(self, year, month)— Like itermonthdates(), but will yield (year, month, day, day_of_week) tuples.Calendar.iterweekdays(self)— Return an iterator for one week of weekday numbers starting with the configured first one.Calendar.monthdatescalendar(self, year, month)— Return a matrix (list of lists) representing a month's calendar.Calendar.monthdays2calendar(self, year, month)— Return a matrix representing a month's calendar.Calendar.monthdayscalendar(self, year, month)— Return a matrix representing a month's calendar.Calendar.setfirstweekday(self, firstweekday)—Calendar.setfirstweekdaymethod/property.Calendar.yeardatescalendar(self, year, width=3)— Return the data for the specified year ready for formatting. The return value is a list of month rows. Each month row contains up to width months.Calendar.yeardays2calendar(self, year, width=3)— Return the data for the specified year ready for formatting (similar to yeardatescalendar()). Entries in the week lists are (day number, weekday number) tuples. Day numbers outside this month areCalendar.yeardayscalendar(self, year, width=3)— Return the data for the specified year ready for formatting (similar to yeardatescalendar()). Entries in the week lists are day numbers.
Usage
import calendar
cal = calendar.Calendar(firstweekday=calendar.SUNDAY)
weeks = cal.monthdayscalendar(2026, 12)
assert len(weeks) >= 4 and all(len(w) == 7 for w in weeks)
Gotchas
- Data-only calendar generator: Produces day numbers (
0for padding days) ordatetime.dateobjects viaitermonthdates().
REPL Inspect
from calendar import Calendar
help(Calendar)
Linked from
- tuts/python3/stdlib_module/calendar
- tuts/python3/stdlib_module/calendar/calendar
- tuts/python3/stdlib_module/calendar/Day
- tuts/python3/stdlib_module/calendar/firstweekday
- tuts/python3/stdlib_module/calendar/HTMLCalendar
- tuts/python3/stdlib_module/calendar/IllegalMonthError
- tuts/python3/stdlib_module/calendar/IllegalWeekdayError
- tuts/python3/stdlib_module/calendar/isleap
- tuts/python3/stdlib_module/calendar/leapdays
- tuts/python3/stdlib_module/calendar/LocaleHTMLCalendar
- tuts/python3/stdlib_module/calendar/LocaleTextCalendar
- tuts/python3/stdlib_module/calendar/month
- tuts/python3/stdlib_module/calendar/Month_class
- tuts/python3/stdlib_module/calendar/monthcalendar
- tuts/python3/stdlib_module/calendar/monthrange
- tuts/python3/stdlib_module/calendar/prcal
- tuts/python3/stdlib_module/calendar/prmonth
- tuts/python3/stdlib_module/calendar/setfirstweekday
- tuts/python3/stdlib_module/calendar/TextCalendar
- tuts/python3/stdlib_module/calendar/timegm
- tuts/python3/stdlib_module/calendar/weekday
- tuts/python3/stdlib_module/calendar/weekheader
Related notes
- tuts/python3/stdlib_module/calendar/calendar Linked here; links to this page; shared tags: #calendar, #stdlib
- tuts/python3/stdlib_module/calendar/Day Linked here; links to this page; shared tags: #calendar, #stdlib
- tuts/python3/stdlib_module/calendar/firstweekday Linked here; links to this page; shared tags: #calendar, #stdlib
- tuts/python3/stdlib_module/calendar/HTMLCalendar Linked here; links to this page; shared tags: #calendar, #stdlib
- tuts/python3/stdlib_module/calendar/IllegalMonthError links to this page; shared tags: #calendar, #stdlib
- tuts/python3/stdlib_module/calendar/IllegalWeekdayError links to this page; shared tags: #calendar, #stdlib
- tuts/python3/stdlib_module/calendar/isleap links to this page; shared tags: #calendar, #stdlib
- tuts/python3/stdlib_module/calendar/leapdays links to this page; shared tags: #calendar, #stdlib
- tuts/python3/stdlib_module/calendar/LocaleHTMLCalendar links to this page; shared tags: #calendar, #stdlib
- tuts/python3/stdlib_module/calendar/LocaleTextCalendar links to this page; shared tags: #calendar, #stdlib