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.

Syntax:
calendar(year)

The below example displays the calendar for the year 2023.

import calendar

print(calendar.calendar(2023))
Output:

 2023 calendar  

Calendar for a specific month

import calendar

yy = 2023
mm = 8

print(calendar.month(yy, mm))
Output:

 

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.

ExampleEdit & Run
# import the calendarmodule
import calendar 

c = calendar.Calendar(firstweekday=0)
year = 2023
month = 8
print(c.monthdatescalendar(year, month))
Output:
[[datetime.date(2023, 7, 31), datetime.date(2023, 8, 1), datetime.date(2023, 8, 2), datetime.date(2023, 8, 3), datetime.date(2023, 8, 4), datetime.date(2023, 8, 5), datetime.date(2023, 8, 6)], [datetime.date(2023, 8, 7), datetime.date(2023, 8, 8), datetime.date(2023, 8, 9), datetime.date(2023, 8, 10), datetime.date(2023, 8, 11), datetime.date(2023, 8, 12), datetime.date(2023, 8, 13)], [datetime.date(2023, 8, 14), datetime.date(2023, 8, 15), datetime.date(2023, 8, 16), datetime.date(2023, 8, 17), datetime.date(2023, 8, 18), datetime.date(2023, 8, 19), datetime.date(2023, 8, 20)], [datetime.date(2023, 8, 21), datetime.date(2023, 8, 22), datetime.date(2023, 8, 23), datetime.date(2023, 8, 24), datetime.date(2023, 8, 25), datetime.date(2023, 8, 26), datetime.date(2023, 8, 27)], [datetime.date(2023, 8, 28), datetime.date(2023, 8, 29), datetime.date(2023, 8, 30), datetime.date(2023, 8, 31), datetime.date(2023, 9, 1), datetime.date(2023, 9, 2), datetime.date(2023, 9, 3)]][Finished in 0.019554106052964926s]

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)
Output:

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.

ExampleEdit & Run
import calendar

html_cal = calendar.HTMLCalendar(firstweekday=0)
year = 2023
month = 8
print(html_cal.formatmonth(year, month))
Output:
<table border="0" cellpadding="0" cellspacing="0" class="month"><tr><th colspan="7" class="month">August 2023</th></tr><tr><th class="mon">Mon</th><th class="tue">Tue</th><th class="wed">Wed</th><th class="thu">Thu</th><th class="fri">Fri</th><th class="sat">Sat</th><th class="sun">Sun</th></tr><tr><td class="noday"> </td><td class="tue">1</td><td class="wed">2</td><td class="thu">3</td><td class="fri">4</td><td class="sat">5</td><td class="sun">6</td></tr><tr><td class="mon">7</td><td class="tue">8</td><td class="wed">9</td><td class="thu">10</td><td class="fri">11</td><td class="sat">12</td><td class="sun">13</td></tr><tr><td class="mon">14</td><td class="tue">15</td><td class="wed">16</td><td class="thu">17</td><td class="fri">18</td><td class="sat">19</td><td class="sun">20</td></tr><tr><td class="mon">21</td><td class="tue">22</td><td class="wed">23</td><td class="thu">24</td><td class="fri">25</td><td class="sat">26</td><td class="sun">27</td></tr><tr><td class="mon">28</td><td class="tue">29</td><td class="wed">30</td><td class="thu">31</td><td class="noday"> </td><td class="noday"> </td><td class="noday"> </td></tr></table>[Finished in 0.018879163078963757s]
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.