Calendar in python



The calendar module in python has features to handle all the features related to calendar and dates. It is one of the very extensively used module which has many in-built functions to use dates in the python programs.

Example

 Live Demo

import calendar
print(calendar.calendar(2019))

Output

Running the above code gives us the following result −

                           2019
   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                     1  2  3                 1 2 3
7 8 9 10 11 12 13       4 5 6 7 8 9 10                4 5 6 7 8 9 10
14 15 16 17 18 19 20    11 12 13 14 15 16 17    11 12 13 14 15 16 17
21 22 23 24 25 26 27    18 19 20 21 22 23 24    18 19 20 21 22 23 24
28 29 30 31             25 26 27 28             25 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 2 3 4 5 6 7                      1 2 3 4 5                   1 2
8 9 10 11 12 13 14       6 7 8 9 10 11 12             3 4 5 6 7 8 9
15 16 17 18 19 20 21    13 14 15 16 17 18 19    10 11 12 13 14 15 16
22 23 24 25 26 27 28    20 21 22 23 24 25 26    17 18 19 20 21 22 23
29 30                   27 28 29 30 31          24 25 26 27 28 29 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 2 3 4 5 6 7                      1 2 3 4                         1
8 9 10 11 12 13 14          5 6 7 8 9 10 11             2 3 4 5 6 7 8
15 16 17 18 19 20 21    12 13 14 15 16 17 18       9 10 11 12 13 14 15
22 23 24 25 26 27 28    19 20 21 22 23 24 25    16 17 18 19 20 21 22
29 30 31                26 27 28 29 30 31       23 24 25 26 27 28 29 30

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                 1 2 3                         1
7  8  9  10 11 12 13     4  5  6  7  8  9 10    2  3  4  5  6  7  8
14 15 16 17 18 19 20    11 12 13 14 15 16 17    9 10 11 12 13 14 15
21 22 23 24 25 26 27    18 19 20 21 22 23 24    16 17 18 19 20 21 22
28 29 30 31             25 26 27 28 29 30       23 24 25 26 27 28 29
                                                30 31

Specific Month

We can get a specific month from the calendar by using the month function with proper parameters.

Example

 Live Demo

import calendar
print (calendar.month(2019,7))

Output

Running the above code gives us the following result −

July 2019
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

Number of Days in a Month

We can find the number of days in a month by using the function monthrange(). It also gives the weekday of the first day of the month. In the below example we find out the number of days in Feb 2019 and also get the weekday number of the first day of February.

Example

 Live Demo

import calendar
print(calendar.monthrange(2019,2))

Output

Running the above code gives us the following result −

(4, 28)

Leap Year

The function isleap() is used to find if a year is leap year or not. We can also use the function leapdays() to calculate the number of leap years between a range of two given years. In the below example we find if the year 2019 and 2020 are leap years or not. And also find the number of leap years between the years 2000 and 2020.

Example

 Live Demo

import calendar
print(calendar.isleap(2019))
print(calendar.isleap(2020))
print(calendar.leapdays(2000, 2020))

Running the above code gives us the following result:

Output

False
True
5

Advertisements