w3resource

Python HTMLCalendar Module : formatmonth() method

formatmonth() method

The formatmonth() method is used to get a month's calendar as an HTML table.

Syntax

formatmonth(theyear, themonth, withyear=True)

Parameters

Name Description Required /
Optional
Type
theyear Year for which the calendar should be generated. Required Number
themonth Month for which the calendar should be generated. Required Number
withyear If withyear is true the year will be included in the header, otherwise just the month name will be used. Optional Boolean

Example of HTML calendar formatmonth() method

import calendar
tc= calendar.HTMLCalendar(firstweekday=0)
print(tc.formatmonth(2016, 5))

Code:


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

Let's run the above HTML code :

Output:

May 2016
MonTueWedThuFriSatSun
      1
2345678
9101112131415
16171819202122
23242526272829
3031     

Previous: pryear()
Next: formatyear()

Test your Python skills with w3resource's quiz



Python: Tips of the Day

What is the difference between Python's list methods append and extend?

append: Appends object at the end.

x = [1, 2, 3]
x.append([4, 5])
print (x)

Output:

[1, 2, 3, [4, 5]]

extend: Extends list by appending elements from the iterable.

x = [1, 2, 3]
x.extend([4, 5])
print (x)

Output:

[1, 2, 3, 4, 5]

Ref: https://bit.ly/2AZ6ZFq