all Python 3calendarterm
calendar.Calendar
calendar classes
The base calendar class: it prints nothing and instead yields the days of a month as iterators and lists of weeks — raw data for your own output.
Syntax
cal = calendar.Calendar(firstweekday=0)
The first weekday belongs to the instance and ignores setfirstweekday(), which only affects the module-level functions. There is no validation: Calendar(7) silently becomes Monday, while setfirstweekday(7) raises IllegalWeekdayError. itermonthdates() also yields days of the neighbouring months so that weeks are complete, whereas itermonthdays() and monthdayscalendar() put 0 in their place.
Examples
import calendar
cal = calendar.Calendar(firstweekday=calendar.SUNDAY)
print(list(cal.iterweekdays())) # → [6, 0, 1, 2, 3, 4, 5]
print(cal.monthdayscalendar(2024, 2)[0]) # → [0, 0, 0, 0, 1, 2, 3]
import calendar
days = list(calendar.Calendar().itermonthdates(2024, 2))
print(days[0], days[-1]) # → 2024-01-29 2024-03-03 — соседние месяцы тоже
print(len(days)) # → 35
import calendar
print(calendar.Calendar(7).firstweekday) # → 0 — без ошибки, по модулю 7
See also
Open in the full glossarydocs.python.orgReport an inaccuracy