Python calendar module : HTMLCalendar formatyear() method (original) (raw)

Last Updated : 13 May, 2022

Calendar module allows to output calendars like program, and provides additional useful functions related to the calendar. Functions and classes defined in Calendar module use an idealized calendar, the current Gregorian calendar extended indefinitely in both directions. class calendar.HTMLCalendar(firstweekday=0) can be used to generate HTML calendars. formatyear() method is one of the methods of HTMLCalendar instance. formatyear() method in Python is used to get year’s calendar as an HTML table.

Syntax: formatyear(year, width=3)

Parameter: year: year of the calendar

**width:**[Default value is 3] Specifies the width date column

Returns: Return an HTML table for an entire year.

Depends on the first weekday as specified in the constructor or set by the setfirstweekday() method.

Code #1:

Python3 `

Python program to demonstrate working of formatyear() method

importing calendar module

import calendar

text_cal = calendar.HTMLCalendar(firstweekday = 0)

year = 2018

Default value of width is 3

printing formatyear

print(text_cal.formatyear(year))

`

Output:

HTML `

2018
January
MonTueWedThuFriSatSun
1234567
891011121314
15161718192021
22232425262728
293031    
February
MonTueWedThuFriSatSun
   1234
567891011
12131415161718
19202122232425
262728    
March
MonTueWedThuFriSatSun
   1234
567891011
12131415161718
19202122232425
262728293031 
April
MonTueWedThuFriSatSun
      1
2345678
9101112131415
16171819202122
23242526272829
30      
May
MonTueWedThuFriSatSun
 123456
78910111213
14151617181920
21222324252627
28293031   
June
MonTueWedThuFriSatSun
    123
45678910
11121314151617
18192021222324
252627282930 
July
MonTueWedThuFriSatSun
      1
2345678
9101112131415
16171819202122
23242526272829
3031     
August
MonTueWedThuFriSatSun
  12345
6789101112
13141516171819
20212223242526
2728293031  
September
MonTueWedThuFriSatSun
     12
3456789
10111213141516
17181920212223
24252627282930
October
MonTueWedThuFriSatSun
1234567
891011121314
15161718192021
22232425262728
293031    
November
MonTueWedThuFriSatSun
   1234
567891011
12131415161718
19202122232425
2627282930  
December
MonTueWedThuFriSatSun
     12
3456789
10111213141516
17181920212223
24252627282930
31      

`

Note that the earliest year for which a calendar can be generated is platform-dependent.

Code #2: width is given 5

Python3 `

Python program to demonstrate working of formatyear() method

importing calendar module

import calendar

text_cal = calendar.HTMLCalendar(firstweekday = 0)

default value of width is 0

printing formatyear

print(text_cal.formatyear(2018, 5))

`

Output:

HTML `

2018
January
MonTueWedThuFriSatSun
1234567
891011121314
15161718192021
22232425262728
293031    
February
MonTueWedThuFriSatSun
   1234
567891011
12131415161718
19202122232425
262728    
March
MonTueWedThuFriSatSun
   1234
567891011
12131415161718
19202122232425
262728293031 
April
MonTueWedThuFriSatSun
      1
2345678
9101112131415
16171819202122
23242526272829
30      
May
MonTueWedThuFriSatSun
 123456
78910111213
14151617181920
21222324252627
28293031   
June
MonTueWedThuFriSatSun
    123
45678910
11121314151617
18192021222324
252627282930 
July
MonTueWedThuFriSatSun
      1
2345678
9101112131415
16171819202122
23242526272829
3031     
August
MonTueWedThuFriSatSun
  12345
6789101112
13141516171819
20212223242526
2728293031  
September
MonTueWedThuFriSatSun
     12
3456789
10111213141516
17181920212223
24252627282930
October
MonTueWedThuFriSatSun
1234567
891011121314
15161718192021
22232425262728
293031    
November
MonTueWedThuFriSatSun
   1234
567891011
12131415161718
19202122232425
2627282930  
December
MonTueWedThuFriSatSun
     12
3456789
10111213141516
17181920212223
24252627282930
31      

`