Python Program for HTMLCalendar formatmonth() Method with Examples

Calendar Module:

The calendar module allows you to output calendars like a program and includes extra calendar-related operations. Calendar module functions and classes make use of an idealized calendar, the current Gregorian calendar extended in both directions indefinitely.

HTMLCalendar formatmonth() Method:

The formatmonth() method is used to generate an HTML table of a month’s calendar.

To generate HTML calendars, class calendar.HTMLCalendar(firstweekday=0) can be used. The formatmonth() method is one of the HTMLCalendar instance methods.

Syntax:

formatmonth(year, month, withyear=True)

Parameter Values:

year: This is required. It is a number. It is the year for which the calendar should be created.

month: This is required. It is a number. It is the month for which the calendar should be created.

withyear: This is Optional. It is Boolean. If withyear is set to true, the year is included in the header; otherwise, only the month name is utilized.

Return Value: A month’s calendar is returned.

Program for HTMLCalendar formatmonth() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Example1:

Approach:

  • Import calendar module using the import keyword.
  • Give the year as static input and store it in a variable.
  • Give the month as static input and store it in another variable.
  • Call the HTMLCalendar() function by setting firstweekday=0 and store it in another variable.
  • Apply formatmonth() method to the above text calendar by passing the given year, month as the arguments and store it in another variable.
  • Print the above result.
  • The Exit of the Program.

Below is the implementation:

# Import calendar module using the import keyword.
import calendar
# Give the year as static input and store it in a variable.
gvn_yr = 2013
# Give the month as static input and store it in another variable.
gvn_mont = 5
# Call the HTMLCalendar() function by setting firstweekday=0 and store it in
# another variable.
txt_calndr = calendar.HTMLCalendar(firstweekday=0)
# Apply formatmonth() method to the above text calendar by passing the given year,
# month,as the arguments and store it in another variable.
rslt = txt_calndr.formatmonth(gvn_yr, gvn_mont)
# Print the above result.
print(rslt)

Output:

<table border="0" cellpadding="0" cellspacing="0" class="month">
<tr><th colspan="7" class="month">May 2013</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">&nbsp;</td><td class="noday">&nbsp;</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">&nbsp;</td><td class="noday">&nbsp;</td></tr>
</table>

Example2:

Approach:

  • Import calendar module using the import keyword.
  • Give the year as static input and store it in a variable.
  • Give the month as static input and store it in another variable.
  • Call the HTMLCalendar() function by setting firstweekday=0 and store it in another variable.
  • Apply formatmonth() method to the above text calendar by passing the given year, month, withyear=True as the arguments and store it in another variable.
  • Print the above result.
  • The Exit of the Program.

Below is the implementation:

# Import calendar module using the import keyword.
import calendar
# Give the year as static input and store it in a variable.
gvn_yr = 2013
# Give the month as static input and store it in another variable.
gvn_mont = 5
# Call the HTMLCalendar() function by setting firstweekday=0 and store it in
# another variable.
txt_calndr = calendar.HTMLCalendar(firstweekday=0)
# Apply formatmonth() method to the above text calendar by passing the given year,
# month,and withyear= True as the arguments and store it in another variable.
rslt = txt_calndr.formatmonth(gvn_yr, gvn_mont, withyear=True)
# Print the above result.
print(rslt)

Output:

<table border="0" cellpadding="0" cellspacing="0" class="month">
<tr><th colspan="7" class="month">May 2013</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">&nbsp;</td><td class="noday">&nbsp;</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">&nbsp;</td><td class="noday">&nbsp;</td></tr>
</table>

Note: 

 If withyear is set to true, the year is included in the header; otherwise,
 only the month name is utilized.

Method #2: Using Built-in Functions (User Input)

Example1:

Approach:

  • Import calendar module using the import keyword.
  • Give the year as user input using the int(input()) function and store it in a variable.
  • Give the month as user input using the int(input()) function and store it in another variable.
  • Call the HTMLCalendar() function by setting firstweekday=0 and store it in another variable.
  • Apply formatmonth() method to the above text calendar by passing the given year, month as the arguments and store it in another variable.
  • Print the above result.
  • The Exit of the Program.

Below is the implementation:

# Import calendar module using the import keyword.
import calendar
# Give the year as user input using the int(input()) function and store it in a variable.
gvn_yr = int(input("Enter some random year = "))
# Give the month as user input using the int(input()) function and store it in another variable.
gvn_mont = int(input("Enter some random month = "))
# Call the HTMLCalendar() function by setting firstweekday=0 and store it in
# another variable.
txt_calndr = calendar.HTMLCalendar(firstweekday=0)
# Apply formatmonth() method to the above text calendar by passing the given year,
# month,as the arguments and store it in another variable.
rslt = txt_calndr.formatmonth(gvn_yr, gvn_mont)
# Print the above result.
print(rslt)

Output:

Enter some random year = 2016
Enter some random month = 3
<table border="0" cellpadding="0" cellspacing="0" class="month">
<tr><th colspan="7" class="month">March 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">&nbsp;</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="thu">31</td><td class="noday">&nbsp;</td><td class="noday">&nbsp;</td><td class="noday">&nbsp;</td></tr>
</table>

Example2:

Approach:

  • Import calendar module using the import keyword.
  • Give the year as user input using the int(input()) function and store it in a variable.
  • Give the month as user input using the int(input()) function and store it in another variable.
  • Call the HTMLCalendar() function by setting firstweekday=0 and store it in another variable.
  • Apply formatmonth() method to the above text calendar by passing the given year, month, withyear=True as the arguments and store it in another variable.
  • Print the above result.
  • The Exit of the Program.

Below is the implementation:

# Import calendar module using the import keyword.
import calendar
# Give the year as user input using the int(input()) function and store it in a variable.
gvn_yr = int(input("Enter some random year = "))
# Give the month as user input using the int(input()) function and store it in another variable.
gvn_mont = int(input("Enter some random month = "))
# Call the HTMLCalendar() function by setting firstweekday=0 and store it in
# another variable.
txt_calndr = calendar.HTMLCalendar(firstweekday=0)
# Apply formatmonth() method to the above text calendar by passing the given year,
# month,and withyear= True as the arguments and store it in another variable.
rslt = txt_calndr.formatmonth(gvn_yr, gvn_mont, withyear=True)
# Print the above result.
print(rslt)

Output:

Enter some random year = 2018
Enter some random month = 4
<table border="0" cellpadding="0" cellspacing="0" class="month">
<tr><th colspan="7" class="month">April 2018</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">&nbsp;</td><td class="noday">&nbsp;</td><td class="noday">&nbsp;</td><td class="noday">&nbsp;</td><td class="noday">&nbsp;</td><td class="noday">&nbsp;</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="noday">&nbsp;</td><td class="noday">&nbsp;</td><td class="noday">&nbsp;</td><td class="noday">&nbsp;</td><td class="noday">&nbsp;</td><td class="noday">&nbsp;</td></tr>
</table>