Python HTMLCalendar formatmonth() Method with Example

Python HTMLCalendar.formatmonth() Method: In this tutorial, we will learn about the formatmonth() method of HTMLCalendar class in Python with its usage, syntax, and examples. By Hritika Rajput Last updated : April 22, 2023

Python HTMLCalendar.formatmonth() Method

The HTMLCalendar.formatmonth() method returns an HTML table which represents the given month's calendar.

Module

The following module is required to use formatmonth() method:

import calendar

Class

The following class is required to use formatmonth() method:

from calendar import HTMLCalendar

Syntax

The following is the syntax of formatmonth() method:

formatmonth(year, month, withyear=True)

Parameter(s)

The following are the parameter(s):

  • year: It is a required parameter, which specifies the year of the calendar
  • month: It is a required parameter, which specifies the month of the calendar
  • withyear: It is an optional parameter, which specifies whether the year will be included in the header, otherwise just the month name will be used; Default value = True.

Return Value

The return type of this method is <class 'str'>, it returns a month's calendar as an HTML table.

Example of HTMLCalendar formatmonth() Method in Python

# Python program to illustrate the 
# use of formatmonth() method

# import class
import calendar

cal = calendar.HTMLCalendar()

year = 2020
month = 2

print(cal.formatmonth(year, month))

Output

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

Example 2

# Python program to illustrate the 
# use of formatmonth() method withyear included

# import class
import calendar

cal = calendar.HTMLCalendar()
year = 2010
month = 12
print(cal.formatmonth(year, month, withyear=False))

Output

<table border="0" cellpadding="0" cellspacing="0" class="month">
    <tr>
        <th colspan="7" class="month">December</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="wed">1</td>
        <td class="thu">2</td>
        <td class="fri">3</td>
        <td class="sat">4</td>
        <td class="sun">5</td>
    </tr>
    <tr>
        <td class="mon">6</td>
        <td class="tue">7</td>
        <td class="wed">8</td>
        <td class="thu">9</td>
        <td class="fri">10</td>
        <td class="sat">11</td>
        <td class="sun">12</td>
    </tr>
    <tr>
        <td class="mon">13</td>
        <td class="tue">14</td>
        <td class="wed">15</td>
        <td class="thu">16</td>
        <td class="fri">17</td>
        <td class="sat">18</td>
        <td class="sun">19</td>
    </tr>
    <tr>
        <td class="mon">20</td>
        <td class="tue">21</td>
        <td class="wed">22</td>
        <td class="thu">23</td>
        <td class="fri">24</td>
        <td class="sat">25</td>
        <td class="sun">26</td>
    </tr>
    <tr>
        <td class="mon">27</td>
        <td class="tue">28</td>
        <td class="wed">29</td>
        <td class="thu">30</td>
        <td class="fri">31</td>
        <td class="noday"> </td>
        <td class="noday"> </td>
    </tr>
</table>



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.