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.
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).