Python Calendar Module | Calendar Class & HTML Calendar
Master Python with 70+ Hands-on Projects and Get Job-ready - Learn Python
We took a brief look at the Python Calendar module when we talked about Python date and time. Let’s take a deeper look at the Calendar Module in Python now.
Moreover, we will talk about three classes in the calendar module- Calendar, TextCalendar, and HTMLCalendar. Also, we will take a look at their available instance methods.
At last, we will learn about the available functions in the Python Calendar Modules.
So, let’s start the Python Calendar Module tutorial.
What is Python Calendar?
Python Calendar Module lets you print calendars and perform functions related to those. These calendars begin at Monday (day=0) and end at Sunday (day=6).
In this Python calendar tutorial, we will notice the Gregorian calendar, set indefinitely in both directions, as the base calendar.
The ISO 8601 standard decides how to interpret zero and negative years. We call year 0 ‘1 BC’, year -1 ‘2 BC’, and so on.
This module holds the Calendar class that lends us various functions to manipulate date and time. Other than that, we have the TextCalendar and HTMLCalendar classes to produce pre-formatted output.
We can import this Python Calendar Module as-
>>> import calendar as cd
Python Calendar Class
The first class we discuss here is the Calendar. The constructor for this has the following syntax:
class calendar.Calendar(firstweekday=0)
This creates a Calendar object with whichever day of the week we want it to begin at. By default, it starts at Monday with a parameter value of 0.
Like we said earlier, Sunday is 6. With the methods of this class, we prepare a calendar for formatting.
An instance of Calendar will have the following methods-
1. iterweekdays() – Days of week
This returns an iterator for the weekday numbers for an entire week. The first number this iterator returns is the number we set as the firstweekday.
>>> import calendar as cd >>> c=cd.Calendar(firstweekday=1) >>> for i in c.iterweekdays(): print(i)
Output
2
3
4
5
6
0
2. itermonthdates(year, month) – Dates of month
This returns an iterator for all days of the month and all days before or after it so we can get a complete week. Let’s set the first weekday to be Monday.
>>> c=cd.Calendar(firstweekday=0) >>> for i in c.itermonthdates(2018,12): print(i)
Output
2018-11-27
2018-11-28
2018-11-29
2018-11-30
2018-12-01
2018-12-02
2018-12-03
2018-12-04
2018-12-05
2018-12-06
2018-12-07
2018-12-08
2018-12-09
2018-12-10
2018-12-11
2018-12-12
2018-12-13
2018-12-14
2018-12-15
2018-12-16
2018-12-17
2018-12-18
2018-12-19
2018-12-20
2018-12-21
2018-12-22
2018-12-23
2018-12-24
2018-12-25
2018-12-26
2018-12-27
2018-12-28
2018-12-29
2018-12-30
2018-12-31
2019-01-01
2019-01-02
2019-01-03
2019-01-04
2019-01-05
2019-01-06
3. itermonthdays(year,month) – Days of month
This gives us an iterator for the month as ‘day of month’. The days outside the month we specify return 0.
>>> for i in c.itermonthdays(2018,12): print(i)
Output
0
0
0
0
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
0
0
0
0
0
0
Notice how the days from months November and January fade into a 0?
4. itermonthdays2(year,month) – Day of month, day of week
This is the same as itermonthdays(), but returns tuples of day of month and day of week.
>>> for i in c.itermonthdays2(2018,12): print(i)
Output
(0, 1)
(0, 2)
(0, 3)
(0, 4)
(1, 5)
(2, 6)
(3, 0)
(4, 1)
(5, 2)
(6, 3)
(7, 4)
(8, 5)
(9, 6)
(10, 0)
(11, 1)
(12, 2)
(13, 3)
(14, 4)
(15, 5)
(16, 6)
(17, 0)
(18, 1)
(19, 2)
(20, 3)
(21, 4)
(22, 5)
(23, 6)
(24, 0)
(25, 1)
(26, 2)
(27, 3)
(28, 4)
(29, 5)
(30, 6)
(31, 0)
(0, 1)
(0, 2)
(0, 3)
(0, 4)
(0, 5)
(0, 6)
5. itermonthdays3(year,month) – Year, month, day of month
This is like the previous two, but gives us a tuple of year, the month, and the day of the month. This is new in Python 3.7.
>>> for i in c.itermonthdays3(2018,12): print(i)
Output
(2018, 11, 27)
(2018, 11, 28)
(2018, 11, 29)
(2018, 11, 30)
(2018, 12, 1)
(2018, 12, 2)
(2018, 12, 3)
(2018, 12, 4)
(2018, 12, 5)
(2018, 12, 6)
(2018, 12, 7)
(2018, 12, 8)
(2018, 12, 9)
(2018, 12, 10)
(2018, 12, 11)
(2018, 12, 12)
(2018, 12, 13)
(2018, 12, 14)
(2018, 12, 15)
(2018, 12, 16)
(2018, 12, 17)
(2018, 12, 18)
(2018, 12, 19)
(2018, 12, 20)
(2018, 12, 21)
(2018, 12, 22)
(2018, 12, 23)
(2018, 12, 24)
(2018, 12, 25)
(2018, 12, 26)
(2018, 12, 27)
(2018, 12, 28)
(2018, 12, 29)
(2018, 12, 30)
(2018, 12, 31)
(2019, 1, 1)
(2019, 1, 2)
(2019, 1, 3)
(2019, 1, 4)
(2019, 1, 5)
(2019, 1, 6)
6. itermonthdays4(year, month) – Year, month, day of month, day of week
Like so far, this returns a tuple with year, month, day of month, and day of week. This is since Python 3.7.
>>> for i in c.itermonthdays4(2018,12): print(i)
Output
(2018, 11, 27, 1)
(2018, 11, 28, 2)
(2018, 11, 29, 3)
(2018, 11, 30, 4)
(2018, 12, 1, 5)
(2018, 12, 2, 6)
(2018, 12, 3, 0)
(2018, 12, 4, 1)
(2018, 12, 5, 2)
(2018, 12, 6, 3)
(2018, 12, 7, 4)
(2018, 12, 8, 5)
(2018, 12, 9, 6)
(2018, 12, 10, 0)
(2018, 12, 11, 1)
(2018, 12, 12, 2)
(2018, 12, 13, 3)
(2018, 12, 14, 4)
(2018, 12, 15, 5)
(2018, 12, 16, 6)
(2018, 12, 17, 0)
(2018, 12, 18, 1)
(2018, 12, 19, 2)
(2018, 12, 20, 3)
(2018, 12, 21, 4)
(2018, 12, 22, 5)
(2018, 12, 23, 6)
(2018, 12, 24, 0)
(2018, 12, 25, 1)
(2018, 12, 26, 2)
(2018, 12, 27, 3)
(2018, 12, 28, 4)
(2018, 12, 29, 5)
(2018, 12, 30, 6)
(2018, 12, 31, 0)
(2019, 1, 1, 1)
(2019, 1, 2, 2)
(2019, 1, 3, 3)
(2019, 1, 4, 4)
(2019, 1, 5, 5)
(2019, 1, 6, 6)
7. monthdatescalendar(year,month) – List of weeks in month
This gives us a list of full weeks in the month we want. Each week is a list of seven datetime.date objects.
>>> for i in c.monthdatescalendar(2018,12): print(i)
Output
[datetime.date(2018, 12, 3), datetime.date(2018, 12, 4), datetime.date(2018, 12, 5), datetime.date(2018, 12, 6), datetime.date(2018, 12, 7), datetime.date(2018, 12, 8), datetime.date(2018, 12, 9)]
[datetime.date(2018, 12, 10), datetime.date(2018, 12, 11), datetime.date(2018, 12, 12), datetime.date(2018, 12, 13), datetime.date(2018, 12, 14), datetime.date(2018, 12, 15), datetime.date(2018, 12, 16)]
[datetime.date(2018, 12, 17), datetime.date(2018, 12, 18), datetime.date(2018, 12, 19), datetime.date(2018, 12, 20), datetime.date(2018, 12, 21), datetime.date(2018, 12, 22), datetime.date(2018, 12, 23)]
[datetime.date(2018, 12, 24), datetime.date(2018, 12, 25), datetime.date(2018, 12, 26), datetime.date(2018, 12, 27), datetime.date(2018, 12, 28), datetime.date(2018, 12, 29), datetime.date(2018, 12, 30)]
[datetime.date(2018, 12, 31), datetime.date(2019, 1, 1), datetime.date(2019, 1, 2), datetime.date(2019, 1, 3), datetime.date(2019, 1, 4), datetime.date(2019, 1, 5), datetime.date(2019, 1, 6)]
To confirm this is correct, check the calendar for December 2018 with Monday as the first day:
The weeks begin at 26, 3, 19, 17, 24, and 31.
8. monthdays2calendar(year,month) – List of day of month and day of week in month
This gives us a list of weeks with each week as seven tuples of day of month and day of the week.
>>> for i in c.monthdays2calendar(2018,12): print(i)
Output
[(3, 0), (4, 1), (5, 2), (6, 3), (7, 4), (8, 5), (9, 6)]
[(10, 0), (11, 1), (12, 2), (13, 3), (14, 4), (15, 5), (16, 6)]
[(17, 0), (18, 1), (19, 2), (20, 3), (21, 4), (22, 5), (23, 6)]
[(24, 0), (25, 1), (26, 2), (27, 3), (28, 4), (29, 5), (30, 6)]
[(31, 0), (0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (0, 6)]
Every week has week numbers 0 to 6 as the second value in the tuples and the first value is the day of month- 0 to 31 here.
9. monthdayscalendar(year,month) – List of day of month
This gives us a list of weeks with each week as a list of day of month.
>>> for i in c.monthdayscalendar(2018,12): print(i)
Output
[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, 0, 0, 0, 0, 0, 0]
10. yeardatescalendar(year,width=3) – List of row months
This gives us a list of month rows. The width parameter is to specify up to how many months we want in a month row, the default for which is 3. The days are datetime.date objects.
>>> for i in c.yeardatescalendar(2018,2): print(i)
11. yeardays2calendar(year,width=3) – List of day of month and day of week
This gives us a list of list of weeks as tuples of day of month and day of week.
>>> for i in c.yeardays2calendar(2018,2): print(i)
12. yeardayscalendar(year,width=3) – List of day of month
This gives us a list of weeks as day of month.
>>> for i in c.yeardayscalendar(2018,2): print(i)
Output
[[[0, 0, 0, 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, 0]], [[0, 0, 0, 0, 0, 0, 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, 0, 0, 0, 0, 0, 0]]]
[[[0, 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, 0, 0, 0]], [[0, 0, 0, 0, 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, 0]]]
[[[0, 0, 0, 0, 0, 0, 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, 0, 0, 0, 0, 0]], [[0, 0, 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, 0, 0]]]
[[[0, 0, 0, 0, 0, 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]], [[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, 0, 0, 0, 0]]]
[[[0, 0, 0, 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, 0, 0]], [[0, 0, 0, 0, 0, 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, 0, 0, 0, 0, 0, 0]]]
Python TextCalendar Class
This class lets us generate plain text calendars. The constructor for this has the following syntax-
class calendar.TextCalendar(firstweekday=0)
Take a look at its available methods-
1. formatmonth(theyear,themonth,[w=0,l=0]) – Multiline string of month
This gives us a month’s calendar as a multiline string. w is the width of centered date columns, l the number of lines for each week- both are optional parameters. We need the value of the firstweekday for this to work.
>>> t=cd.TextCalendar(0) >>> t.formatmonth(2018,12)
Output
‘   December 2018\nMo Tu We Th Fr Sa Su\n                1 2\n 3 4 5 6 7 8 9\n10 11 12 13 14 15 16\n17 18 19 20 21 22 23\n24 25 26 27 28 29 30\n31\n’
>>> print(t.formatmonth(2018,12))
2. prmonth(theyear,themonth,[w=0,l=0]) – Print month calendar
What we printed last, we can also do with prmonth() instead.
>>> t.prmonth(2018,12)
3. formatyear(theyear,[w=2,l=1,c=6,m=3]) – Multiline string calendar
This returns an m-column calendar for the entire year as a multiline string. Optional parameters- w for date column width, l for lines per week, c for number of spaces between month columns.
For this to work, we need the firstweekday. The earliest year you can generate a calendar for depends on your platform.
>>> print(t.formatyear(2018,5)) #sets w=5 and m is 3 by default >>> print(t.formatyear(2018,m=5))
4. pryear(theyear,[w=2,l=1,c=6,m=3]) – Print year calendar
This prints the same thing as above.
>>> t.pryear(2018,m=5)
Python HTMLCalendar
Finally, let’s talk about the class that lets us generate HTML Calendars in Python. The constructor for this has the following syntax:
class calendar.HTMLCalendar(firstweekday=0)
An HTML Python Calendar instance has the following methods:
1. formatmonth(theyear,themonth,withyear=True) – HTML table for month calendar
This gives us the calendar for a month as an HTML table
>>> h=cd.HTMLCalendar() >>> print(h.formatmonth(2018,12))
Output
<tr><th colspan=”7″ class=”month”>December 2018</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=”noday”> </td><td class=”noday”> </td><td class=”noday”> </td><td class=”noday”> </td><td class=”sat”>1</td><td class=”sun”>2</td></tr>
<tr><td class=”mon”>3</td><td class=”tue”>4</td><td class=”wed”>5</td><td class=”thu”>6</td><td class=”fri”>7</td><td class=”sat”>8</td><td class=”sun”>9</td></tr>
<tr><td class=”mon”>10</td><td class=”tue”>11</td><td class=”wed”>12</td><td class=”thu”>13</td><td class=”fri”>14</td><td class=”sat”>15</td><td class=”sun”>16</td></tr>
<tr><td class=”mon”>17</td><td class=”tue”>18</td><td class=”wed”>19</td><td class=”thu”>20</td><td class=”fri”>21</td><td class=”sat”>22</td><td class=”sun”>23</td></tr>
<tr><td class=”mon”>24</td><td class=”tue”>25</td><td class=”wed”>26</td><td class=”thu”>27</td><td class=”fri”>28</td><td class=”sat”>29</td><td class=”sun”>30</td></tr>
<tr><td class=”mon”>31</td><td class=”noday”> </td><td class=”noday”> </td><td class=”noday”> </td><td class=”noday”> </td><td class=”noday”> </td><td class=”noday”> </td></tr>
</table>
2. formatyear(theyear,width=3) – HTML table for year calendar
This gives us an HTML table for an entire year’s calendar. The width gives us the number of months in a row and defaults to 3.
>>> print(h.formatyear(2018,5))
3. formatyearpage(theyear,width=3,css=’calendar.css’,encoding=None)
This gives us an entire year’s calendar as an HTML page with the width defaulting to 3. If we use no cascading style sheet, we can put None there.
>>> print(h.formatyearpage(2018,css=None))
Functions in the Python Calendar Module
So far, we have seen two subclasses of the Python Calendar Class- TextCalendar and HTMLCalendar. Now, let’s take a look at all the functions we have with the Python Calendar Module.
1. calendar.setfirstweekday(weekday) – Set a day to begin the week at
This lets us set which day of the week to begin a week at. 0 is Monday and 6 is Sunday.
>>> t.setfirstweekday(cd.SUNDAY) >>> print(t.formatyear(2018,5))
2. calendar.firstweekday – First day of week
This gives us the first day of the week.
>>> t.firstweekday
Output
3. calendar.isleap(year) – Whether a year is leap
This tells us whether a year is leap.
>>> cd.isleap(2018)
Output
4. calendar.leapdays(y1,y2) – Number of leap years in range
From year y1 to year y2, this gives us the number of leap years.
>>> cd.leapdays(1995,2018)
Output
5. calendar.weekday(year,month,day) – Day of week
This returns the day of week where 0 is Monday.
>>> cd.weekday(2018,12,31)
Output
6. calendar.weekheader(n) – Abbreviated weekday names
This gives us a header holding abbreviated weekday names. w is the width in characters for one weekday.
>>> cd.weekheader(1)
Output
>>> cd.weekheader(2)
Output
>>> cd.weekheader(3)
Output
7. calendar.monthrange(year,month) – First day of month, number of days
This gives us the first day of the month and the number of days
>>> cd.monthrange(2018,12)
Output
8. calendar.monthcalendar(year,month) – Matrix of month calendar
This gives us a matrix for a month’s calendar.
>>> for i in cd.monthcalendar(2018,12): print(i)
Output
[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, 0, 0, 0, 0, 0]
9. calendar.prmonth(theyear,themonth,w=0,l=0) – Print multiline month
This prints the month that calendar.month gives us.
>>> cd.prmonth(2018,12)
10. calendar.month(theyear,themonth,w=0,l=0) – Multiline month string
This gives us a month’s calendar in a multiline string. For this, it uses the formatmonth() method from TextCalendar.
>>> cd.month(2018,12)
Output
11. calendar.prcal(year,w=0,l=0,c=6,m=3) – Print year calendar
This prints the calendar of an entire year- one that calendar.calendar returns.
>>> cd.prcal(2018)
12. calendar.calendar(year,w=2,l=1,c=6,m=3) – Multiline string year calendar
This gives us a 3-column year calendar as a multiline string. For this, it uses the formatyear() method from TextCalendar.
>>> cd.calendar(2018)
Output
13. calendar.timegm(tuple) – Timestamp value from time tuple
This take a time tuple and returns a timestamp value with the epoch being 1970 and with a POSIX encoding. This is the inverse of time.gmtime().
>>> import time >>> cd.timegm(time.gmtime(30000))
Output
So, this was all in Python calendar Module Tutorial. Hope you like our explanation.
Python Interview Questions on Calendar Module
- What is Calendar Module in Python?
- How to use Python Calendar Module?
- How to print a whole year Calendar in Python?
- How to make a Calendar program in Python?
- How to import a Calendar Module in Python?
Conclusion
Hence, in this Python Calendar tutorial, we saw Calendar Module in Python. Moreover, we discussed Python Calendar example to understand the module easily.
Also, we look at TextCalendar and HTML Calendar in Python. At last, we learned the functions in Python calendar Module.
Your opinion matters
Please write your valuable feedback about DataFlair on Google