The calendar module in the standard library offer tools to manipulate dates and times specifically year, month and day and week values. 

At the heart of the tools offered by the module, are several classes that provide ways to calculate, represent and format different calendar-related values. We will explore this classes in details in a while.

Displaying Calendar values

The  calendar method returns a calendar object representing calendar details for a given year.


The below example displays the calendar for the year 2023.

import calendar


 2023 calendar  

Calendar for a specific month

import calendar

yy = 2023
mm = 8

print(calendar.month(yy, mm))


The Calendar class

The Calendar class encapsulates operations that can be used for comparing, calculating, formatting and parsing dates. The class can also be used to store dates in any calendar system, such as the Julian or Gregorian calendar systems, or a completely custom calendar system. The class is actually a base class for several other classes in the module.

# import the calendarmodule
import calendar 

c = calendar.Calendar(firstweekday=0)
year = 2023
month = 8
print(c.monthdatescalendar(year, month))

The methods defined by the class are as shown below:

Method Usage
iterweekdays() For all the week day numbers that would be used for one week, one iterator is returned
itermonthdates() An iterator for all the months from 1 - 12 in the year is returned
itermonthdays() An iterator of the month and year specified
itermonthdays2() Similar to the itermonthdays(), however it returns days in the form of tuples that consist of the day of the month and week day number
itermonthdays3() Similar to itermonthdates() but it returns days in the form of tuples consisting year, month and day of the month numbers
monthdatescalendar() A list of the weeks of a particular month, with each week being a list of datetime.time objects, which are 7 in number (7 days in a week).
monthdays2calendar() Same as monthdatescalendar() but  weeks are returned as tuples of day and week numbers
monthdayscalendar() A list of weeks in the particular month of that year is returned. Here, the weeks are lists of the 7 day numbers
yeardatescalendar() A list of month rows is returned which is essentially data for a particular year, ready to be formatted
yeardays2calendar() Similar to the yearsdatescalendar() function however, the weeks are formatted in the form of tuples of day and weekday numbers
yeardayscalendar() Similar to the yeardatescalendar() , however, any day numbers that are outside this month have the value 0

The TextCalendar class

The TextCalendar class in the module is be used to create plain text calendars as strings. It is a subclass and  works similarly to the Calendar class. The class also includes formatting methods, such as the prmonth() method which will generate the same calendar as the regular Calendar class but as a string.

import calendar

text_cal = calendar.TextCalendar(firstweekday=0)
year = 2023
month = 8

text_cal.formatmonth(year, month, w=0, l=0)
text_cal.prmonth(year, month, w=0, l=0)

The formatmonth() method customizes the output of the calendar for a given month.

The following part shows the various methods defined by the class, without the ones inherited from the Calendar class.

Method Usage
formatmonth(year, month, width=0,  length =0) Returns a multi-line string for the represented calendar values.
formatweek(year, month, day) Returns a single week calendar as  a string.
formatyear(year, w=2, l=1, c=6, m=3) The whole year's calendar as a string.
prmonth(year, month, w=0, l=0) Prints single month’s calendar to stdout.
pryear(year, w=2, l=1, c=6, m=3) Prints a year’s calendar to stdout.

The HTMLCalendar Class

The HTMLCalendar class provides an easy way to generate calendars formatted in  HTML format. It also provides methods to further format the HTML calendar and apply custom styles. Like the TextCalendar, this class is also a subclass of the Calendar class.

import calendar

html_cal = calendar.HTMLCalendar(firstweekday=0)
year = 2023
month = 8
print(html_cal.formatmonth(year, month))
August 2023
Mon Tue Wed Thu Fri Sat Sun
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31      

The methods defined by the class are as follows, without including the ones inherited from the Calendar class:

method Usage
formatmonth() This method is used to get the calendar object of a month in the form of a multi-line string.
prmonth() This method is  used to print the calendar object returned to the formatmonth() method to the stdout.
formatyear() Similar to the formatmonth() method, this allows us to get the calendar of the entire year, with m columns (you are required to specify m in the input parameters)
pryear() This method prints the calendar object returned by  formatyear() to the stdout.