Example HTML page

The Calendar Module

The Calendar Module

The calendar module provides calendar-related functions, and also includes the functions to print a text calendar for a given month or year.

To import a calendar module

>>> import calendar
>>> dir(calendar)
['Calendar', 'EPOCH', 'FRIDAY', 'February', 'HTMLCalendar', 'IllegalMonthError', 'IllegalWeekdayError', 'January', 'LocaleHTMLCalendar', 'LocaleTextCalendar', 'MONDAY', 'SATURDAY', 'SUNDAY', 'THURSDAY', 'TUESDAY', 'TextCalendar', 'WEDNESDAY', '_EPOCH_ORD', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_colwidth', '_locale', '_localized_day', '_localized_month', '_spacing', 'c', 'calendar', 'datetime', 'day_abbr', 'day_name', 'different_locale', 'error', 'firstweekday', 'format', 'formatstring', 'isleap', 'leapdays', 'main', 'mdays', 'month', 'month_abbr', 'month_name', 'monthcalendar', 'monthrange', 'prcal', 'prmonth', 'prweek', 'setfirstweekday', 'sys', 'timegm', 'week', 'weekday', 'weekheader']

Functions/methods of calendar module:

The following are some of the functions/methods of Calendar module.

1. calendar.firstweekday ( )

This returns the current setting for the weekday that starts each week. By default, when calendar is first imported, this is 0, meaning Monday.

Ex

>>> calendar.firstweekday( )

0

2.calendar.isleap(year)

This returns true if the given year is a leap year; otherwise, it returns false.

Ex

>>> calendar.isleap(2004)

True

>>> calendar.isleap(2018)

False

>>> year=int(input(“Enter year: “))

Enter year: 1989

>>> calendar.isleap(year)

False

3.calendar.leapdays(y1,y2)

This returns the total number of leap days in the years within range(y1,y2).

Ex

>>> calendar.leapdays(1989,1993)

1

>>> calendar.leapdays(2000,2018)

5

4.calendar.month(year,month,w=2,l=1)

This returns a multi-line string with a calendar for month of year, one line per week plus two header lines. w is the width in characters of each date; each line has length 7*w+6. l is the number of lines for each week.

Ex

>>> calendar.month(2018,8,w=3,l=2)
'        August 2018\n\nMon Tue Wed Thu Fri Sat Sun\n\n          1   2   3   4   5\n\n  6   7   8   9  10  11  12\n\n 13  14  15  16  17  18  19\n\n 20  21  22  23  24  25  26\n\n 27  28  29  30  31\n\n'
>>> print(calendar.month(2018,8,w=3,l=2))
        August 2018

Mon Tue Wed Thu Fri Sat Sun

          1   2   3   4   5

  6   7   8   9  10  11  12

 13  14  15  16  17  18  19

 20  21  22  23  24  25  26

 27  28  29  30  31

5.calendar.monthrange(year,month)

This returns two integers. The first one is the code of the weekday for the first day of the month month in year year; the second one is the number of days in the month. Weekday codes are 0 (Monday) to 6 (Sunday); month numbers are 1 to 12.

Ex

>>> calendar.monthrange(2018,8)

(2, 31)

>>> calendar.monthrange(2018,2)

(3, 28)

6.calendar.prcal(year,month,w=2,l=1)

It is similar to as print calendar.calendar(year,w,l,c).

Ex:

>>> calendar.prcal(2018,w=2,l=1,c=5)
                                 2018

      January                  February                  March
Mo Tu We Th Fr Sa Su     Mo Tu We Th Fr Sa Su     Mo Tu We Th Fr Sa Su
 1  2  3  4  5  6  7               1  2  3  4               1  2  3  4
 8  9 10 11 12 13 14      5  6  7  8  9 10 11      5  6  7  8  9 10 11
15 16 17 18 19 20 21     12 13 14 15 16 17 18     12 13 14 15 16 17 18
22 23 24 25 26 27 28     19 20 21 22 23 24 25     19 20 21 22 23 24 25
29 30 31                 26 27 28                 26 27 28 29 30 31

       April                     May                      June
Mo Tu We Th Fr Sa Su     Mo Tu We Th Fr Sa Su     Mo Tu We Th Fr Sa Su
                   1         1  2  3  4  5  6                  1  2  3
 2  3  4  5  6  7  8      7  8  9 10 11 12 13      4  5  6  7  8  9 10
 9 10 11 12 13 14 15     14 15 16 17 18 19 20     11 12 13 14 15 16 17
16 17 18 19 20 21 22     21 22 23 24 25 26 27     18 19 20 21 22 23 24
23 24 25 26 27 28 29     28 29 30 31              25 26 27 28 29 30
30

        July                    August                 September
Mo Tu We Th Fr Sa Su     Mo Tu We Th Fr Sa Su     Mo Tu We Th Fr Sa Su
                   1            1  2  3  4  5                     1  2
 2  3  4  5  6  7  8      6  7  8  9 10 11 12      3  4  5  6  7  8  9
 9 10 11 12 13 14 15     13 14 15 16 17 18 19     10 11 12 13 14 15 16
16 17 18 19 20 21 22     20 21 22 23 24 25 26     17 18 19 20 21 22 23
23 24 25 26 27 28 29     27 28 29 30 31           24 25 26 27 28 29 30
30 31

      October                  November                 December
Mo Tu We Th Fr Sa Su     Mo Tu We Th Fr Sa Su     Mo Tu We Th Fr Sa Su
 1  2  3  4  5  6  7               1  2  3  4                     1  2
 8  9 10 11 12 13 14      5  6  7  8  9 10 11      3  4  5  6  7  8  9
15 16 17 18 19 20 21     12 13 14 15 16 17 18     10 11 12 13 14 15 16
22 23 24 25 26 27 28     19 20 21 22 23 24 25     17 18 19 20 21 22 23
29 30 31                 26 27 28 29 30           24 25 26 27 28 29 30
                                                  31

7.calendar.prmonth(year,month,w=2,l=1)

It is similar to calendar.month(year,month,w,l). Prints specific month.

Ex:

>>> calendar.month(2018,12,w=2,l=1)
'   December 2018\nMo Tu We Th Fr Sa Su\n                1  2\n 3  4  5  6  7  8  9\n10 11 12 13 14 15 16\n17 18 19 20 21 22 23\n24 25 26 27 28 29 30\n31\n'
>>> calendar.prmonth(2018,12,w=2,l=1)
   December 2018
Mo Tu We Th Fr Sa Su
                1  2
 3  4  5  6  7  8  9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31

8.calendar.weekday(year,monthday)

This returns the weekday code for the given date. Weekday codes are 0 (Monday) to 6 (Sunday); month numbers are 1 (January) to 12 (December).

Ex

>>> calendar.weekday(1989,7,21)

4

Example programs:

1. A python program to print a HTML calendar.

import calendar
html_cal=calendar.HTMLCalendar(calendar.MONDAY)
str=html_cal.formatmonth(2020,2)
print(str)

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">&nbsp;</td><td class="noday">&nbsp;</td><td class="noday">&nbsp;</td><td class="noday">&nbsp;</td><td class="noday">&nbsp;</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">&nbsp;</td></tr>
</table>

2. A python program to print the names of the months.

import calendar
for months in calendar.month_name:
    print(months)

Output

January

February

March

April

May

June

July

August

September

October

November

December

3.A python program to print the weekdays.

import calendar
for days in calendar.day_name:
    print(days)

Output

Monday

Tuesday

Wednesday

Thursday

Friday

Saturday

Sunday

4.A Python program to display calendar of particular month of the year.

import calendar
yy = int(input("Enter year: "))
mm = int(input("Enter month: "))
print(calendar.month(yy, mm))

Output

Enter year: 1989
Enter month: 7
     July 1989
Mo Tu We Th Fr Sa Su
                1  2
 3  4  5  6  7  8  9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31

 

Example HTML page

Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest