Generic selectors
Exact matches only
Search in title
Search in content
wb_sunny

Python Calendar module – 6 IMP functions to know!

Python Calendar Module

Hello, readers! In this article, we will be focusing on Python Calendar module in detail. So, let us get started!!


First, what is the calendar module?

Python provides us with various modules to deal with the data, make manipulations and alterations and have it presented in a formatted manner as well.

With the calendar module in Python, we can easily represent the data related to dates in a calendar-like format. Moreover, it offers us a number of functions to work on the datestamp-related data, make manipulations and get the desired data out of it.

The calendar module helps us manipulate and represent the data in terms of date, year, month, days, and other attributes such as weeks of the months, leap year, etc.

Functions of the calendar module in Python

We’ll cover the following functions offered by Python calendar module:

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

Let us now have a look at each one of them in the upcoming section!


1. The calendar.month() function

With the calendar module, we can easily represent the calendar of the entire month in a readable calendar format. The calendar.month() function enables us to represent the calendar of the desired month. For the same, we need to pass the month and the year value to the month() function.

Have a look at the below syntax!

calendar.month(year, month)

Example:

In this example, we have passed the year = 2020 and month = 8 i.e. August. Thus, the month() function returns a calendar of August month, 2020.

import calendar
year = 2020
month = 8  
print(calendar.month(year, month)) 

Output:

     August 2020
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
31

2. The calendar.calendar() function

Apart from representing the month’s calendar, with the calendar module in Python, we can even represent the calendar of the entire year in a customized manner.

Syntax:

calendar.calendar(year)

Example:

In this example, we have represented the entire calendar of the year 2020 using calendar.calendar() function.

import calendar
year = 2020
print(calendar.calendar(year))

Output:

Python Calendar Module
Python Calendar Module

3. Represent month in a HTML format

The calendar module can represent data of a particular month or a year in HTML format as well. Thus, the desired month of the year gets represented in HTML format with the tags associated with it.

Syntax:

calendar.HTMLCalendar(firstweekday = 0)

Example:

With calendar.HTMLCalendar() function, we can generate HTML instances of the calendar. Further, formatmonth() function enables us to generate the HTML calendar of the desired month of the year. Setting withyear = TRUE, allows us to include the year value in the header of the HTML format.

import calendar 

cal = calendar.HTMLCalendar(firstweekday = 0) 

print(cal.formatmonth(2020, 9, withyear = True)) 

Output:

<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>
</table>

4. The calendar.isleap() function

With the calendar.isleap() function, we can check whether a particular year is a leap year or not.

Syntax:

calendar.isleap(year)

Example:

In the below example, we have checked if the year ‘2020’ and ‘2002’ is a leap year or not. As isleap() is a boolean function, it returns TRUE if the year is a leap year, else it returns FALSE.

import calendar 

print(calendar.isleap(2020)) 
print(calendar.isleap(2002)) 

Output:

True
False

5. The calendar.leapdays() function

With leapdays() function, we can easily count and represent the number of leap days between the years passed to the function.

Syntax:

calendar.leapdays(year1, year2)

Example:

Here, we have tried to count the number of leap days between the year 2016 and 2020 as well as 2002 and 2003. It counts the number of leap days between these years and returns the integer value.

import calendar 

print(calendar.leapdays(2016, 2020)) 
print(calendar.leapdays(2002, 2003)) 

Output:

1
0

Conclusion

Feel free to comment below, in case you come across any questions. For more such posts related to Python Programming, stay tuned with us. Till then, Happy Learning!! 🙂