ADVERTISEMENT
ADVERTISEMENT

Python HTMLCalendar Class | formatmonth() Method with Example

Python HTMLCalendar.formatmonth() Method: Here, we are going to learn about the formatmonth() method of HTMLCalendar class in Python with its definition, syntax, and examples.
Submitted by Hritika Rajput, on May 09, 2020

Python HTMLCalendar.formatmonth() Method

formatmonth() method is an inbuilt method of the HTMLCalendar class of calendar module in Python. It works on HTMLCalendar class object and returns an HTML table which represents the given month's calendar.

Module:

    import calendar

Class:

    from calendar import HTMLCalendar

Syntax:

    formatmonth(year, month, withyear=True)

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:

# 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:

# 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>
ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.