Python Reference ยท Standard Library: Math, Data and Text
The calendar module
Reference for Python's calendar module: print text calendars, check leap years, find a month's length and first weekday, iterate weeks and use constants.
The calendar module answers calendar questions that datetime does not: is this a leap year, how many days does February have, which weekday does a month start on, and what does the month look like on paper. It can print plain-text calendars and generate HTML ones.
By default weeks start on Monday and weekday numbers run from 0 (Monday) to 6 (Sunday), matching datetime.date.weekday(). You can change the first day with calendar.setfirstweekday() or by creating a TextCalendar with your own value.
import calendar print(calendar.isleap(2032)) # True print(calendar.monthrange(2030, 2)) # (4, 28)
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.
Main functions and classes
| Name | Description |
|---|---|
calendar.isleap(year) |
True for leap years (divisible by 4, except centuries not divisible by 400). |
calendar.leapdays(y1, y2) |
Number of leap years in the range y1 up to but not including y2. |
calendar.monthrange(year, month) |
Tuple (weekday of day 1, days in month). |
calendar.weekday(year, month, day) |
Weekday number, Monday = 0. |
calendar.month(year, month) |
A multi-line string with one month. calendar.calendar(year) gives the whole year. |
calendar.prmonth() / prcal() |
Print the same text. |
calendar.monthcalendar(year, month) |
List of weeks, each a list of seven day numbers (0 for days outside the month). |
calendar.timegm(tuple) |
Converts a UTC time tuple to a timestamp (the inverse of time.gmtime). |
Calendar, TextCalendar, HTMLCalendar |
Classes with iteration helpers, text output and HTML output. |
day_name, day_abbr, month_name, month_abbr |
Sequences of localized names. month_name[0] is an empty string. |
MONDAY ... SUNDAY |
Constants 0 to 6. |
Usage notes
- Months are 1 to 12, weekdays 0 to 6. An invalid month raises
calendar.IllegalMonthError, a subclass ofValueErrorandIndexError. - Index
month_namewith the month number directly, because its first entry (index 0) is empty.day_namehas no such offset. - Week lists contain zeros for days belonging to neighboring months. Use
Calendar.itermonthdates()to get realdateobjects instead. calendar.timegmtreats the tuple as UTC, whereastime.mktimetreats it as local time.- Proleptic Gregorian. Dates before 1582 are calculated as though the Gregorian calendar had always existed.
- Name languages depend on the locale, so
day_namecan differ between machines. - Counting business days is not built in. Combine
weekday()with a loop or a set of holidays.
Related
datetime,time,ValueError- Lessons: Dates and times