Python Calendar Module – 4 IMP functions to know!

Filed Under: Python Modules
Python CALENDAR Module (1)

Hey, readers! In this article, we will be focusing on Python Calendar Module, in detail.

So, let us begin!! 馃檪

Functioning of Calendar module in Python

Python being a vast programming language, it has its implementation in various IT domains. It offers us with various modules to manipulate and deal with the data with reference to its utilization in various different domains and applications.

One such module is Python Calendar module.

When it comes to application programming, we often feel the need to have date related current system information for the application. At times, we have to configure it by having separate code to get the data related to date and time, leap years, calendar of the year, etc.

With Python calendar module, we can have all of the above requirements using one line of command (in-built module functions).

Yes, Python calendar module offers us with various functions with which we can manipulate the data in terms of date, time and the months as well.

In the course of this topic, we will be focusing on the Functioning of Calendar module. Also, some of the mostly used functions offered by the same.

Functionalities offered by Calendar module

We will be have a look at the below functions offered by the Calendar module in Python–

  1. calendar.month() function
  2. calendar.calendar() function
  3. monthdatescalendar() function
  4. isleap() function
  5. leapdays() function

Let us have a look at the first one!

Recommended read: Python time module

1. Get calendar for the month

In order to have a full fledged calendar for any month, we can make use of calendat.month() function.

With month() function, we can get a calendar format value for the entire month for the specified year.


calendar.month(year, month)


In this example, we have requested for the calendar of month 5 (May) from the year 2021.

import calendar
year = 2021
month = 5
print(calendar.month(year, month)) 


      May 2021      
Mo Tu We Th Fr Sa Su
                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

2. Calendar of a month as HTML code

With calendar.HTMLCalendar() function, we can get the entire calendar for a specific month as an HTML code. This HTML code can then be re-used into any html page to view the calendar for the month. By this, it introduces the concept of reusability within different languages altogether.


calendar.HTMLCalendar(firstweekday = 0)


HTMLCalendar() function enables us to have instances of HTML calendar code. To have it for a particular month, we need to combine it with formatmonth() function to get the html code for calendar of specific month–

import calendar 
cal = calendar.HTMLCalendar(firstweekday = 0) 
print(cal.formatmonth(2020, 9, withyear = True))


<table border="0" cellpadding="0" cellspacing="0" class="month">
<tr><th colspan="7" class="month">September 2020</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="noday">聽</td><td class="noday">聽</td><td class="noday">聽</td><td class="noday">聽</td></tr>

3. Leap Year/Day quick check!

With Python Calendar module, we can check if a particular year is leap or not using isleap() function. If the year is a leap year, it returns true. Else, it returns false.

Also, we can get the total number of leap days between leap years using leapdays() function. It counts and returns the number of leap days between the specified years.


calendar.leapdays(year1, year2)


As seen below, the leapdays() function returns 1 as there is one leap day that was encountered between 2016-2020.

As, 2002 was not a leap year, the isleap() function returns False.

import calendar 
print(calendar.leapdays(2016, 2020)) 



4. Get the entire calendar!

Yes, with Python calendar module, we can get the entire calendar in our IDE/CMD. For the same, we make use of calendar() function.

It returns the calendar for the specific year we request for.




import calendar
year = 2021


Image 4
Calendar – Year


By this, we have come to the end of this topic. Feel free to comment below, in case you come across any question.

For more such posts related to Python, stay tuned with us!

Till then, Happy Learning! 馃檪

Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors