Calendar function in Python

PythonProgrammingServer Side Programming

Python has an in built module called calendar which operation is related to calendar. There are some calendar functions in Python.

calendar(year, w, l, c)

This function shows the year, width of characters, no. of lines per week and column separations.

Example

print ("The calendar of 2014 is : ") 
print (calendar.calendar(2014,3,1,4))

Output

The calendar of year 2014 is : 
    2014
   January                        		February                  March
Mon Tue Wed Thu Fri Sat Sun    Mon Tue Wed Thu Fri Sat Sun    Mon Tue Wed Thu Fri Sat Sun
         1   2   3   4   5                          1   2                          1   2
 6   7   8   9  10  11  12      3   4   5   6   7   8   9     3   4   5   6   7    8   9
13  14  15  16  17  18  19     10  11  12  13  14  15  16     10  11  12  13  14  15  16
20  21  22  23  24  25  26     17  18  19  20  21  22  23     17  18  19  20  21  22  23
27  28  29  30  31             24  25  26  27  28             24  25  26  27  28  29  30
				                                                  31

      April                           	May                            	June
Mon Tue Wed Thu Fri Sat Sun    Mon Tue Wed Thu Fri Sat Sun    Mon Tue Wed Thu Fri Sat Sun
     1   2   3   4   5   6                  1   2   3   4                              1
 7   8   9  10  11  12  13      5   6   7   8   9  10  11      2   3   4   5   6   7   8
14  15  16  17  18  19  20     12  13  14  15  16  17  18      9  10  11  12  13  14  15
21  22  23  24  25  26  27     19  20  21  22  23  24  25     16  17  18  19  20  21  22
28  29  30                     26  27  28  29  30  31         23  24  25  26  27  28  29
                                                              30

      July                          	August                       	September
Mon Tue Wed Thu Fri Sat Sun    Mon Tue Wed Thu Fri Sat Sun    Mon Tue Wed Thu Fri Sat Sun
	  1   2   3   4   5   6                      1   2   3      1   2   3   4   5   6   7
 7   8   9  10  11  12  13      4   5   6   7   8   9  10      8   9  10  11  12  13  14
14  15  16  17  18  19  20     11  12  13  14  15  16  17     	15  16  17  18  19  20  21
21  22  23  24  25  26  27     18  19  20  21  22  23  24     	22  23  24  25  26  27  28
28  29  30  31                 25  26  27  28  29  30  31     	29  30

       October                       		 November                     December
Mon Tue Wed Thu Fri Sat Sun    Mon Tue Wed Thu Fri Sat Sun    Mon Tue Wed Thu Fri Sat Sun
         1   2   3   4   5                   	   1   2      1   2   3   4   5   6   7
6   7   8   9  10  11  12      3   4   5   6   7   8   9      8   9  10  11  12  13  14
13  14  15  16  17  18  19     10  11  12  13  14  15  16     15  16  17  18  19  20  21
20  21  22  23  24  25  26     17  18  19  20  21  22  23     22  23  24  25  26  27  28
27  28  29  30  31             24  25  26  27  28  29  30     29  30  31

firstweekday()

This function returns the first day of the week.

Example

print ("The starting day in calendar is : ",end="") 
print (calendar.firstweekday()) 

Output

The starting day in calendar is :  0

isleap (year)

This function checks if mentioned year in argument is leap year or not.

Example

if (calendar.isleap(2014)): 
   print ("The year is leap year") 
else : 
   print ("The year is not leap year")

Output

The year is not leap year.

leapdays (year1, year2)

This function calculates the number of leap (year)days between the year specified in arguments.

Example

print ("The leap (year) days between 1950 and 2000 are : ",end="") 
print (calendar.leapdays(1950, 2000)) 

Output

The leap days between 1950 and 2000 are : 12

month (year, month, w, l)

This function display the month of a specific mentioned year in arguments. It takes 4 parameters year, month, and width of characters and no. of lines taken by a week.

Example

print ("The month 6th of 2017 is :") 
print (calendar.month(2017,6,3,1)) 

Output

The month 6th of 2017 is :
         June 2017
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
raja
Published on 26-Sep-2018 14:00:00
Advertisements