Search Here

Demystifying Python Calendar Module with Examples for beginners

Demystifying Python Calendar Module with Examples for beginners

Table of Contents

Introduction

Python Calendar module provides methods for better-representing dates, weekdays, and months. The class Calendar is quite helpful and displays all information to the user while working with days and calendar problems. In this post, we’ll solve and explain examples of all the methods provided in class Calendar.

Below is a snippet of including the calendar module into your script.

        
import calender 

Creating Calendar object from class Calendar

creating a calendar object

calendar.Calendar(firstweekday=0)

firstweekday the argument is for setting starting weekday of month 0 is for Monday and 6 is for Sunday.

We can also update the firstweekday by assigning value to it show below

        
calendar.Calendar(firstweekday=0)
firstweekday=6

Calendar module also provides getter and setter function to assign a new value to firstweekday.

        
calendar.Calendar(firstweekday=0)
firstweekday=6
print(my_calendar.getfirstweekday())

#PYTHON OUTPUT
6

----------------------
calendar.Calendar(firstweekday=0)
my_calendar.setfirstweekday(3)
print(my_calendar.getfirstweekday())

#PYTHON OUTPUT
3

Calendar class Methods

Calendar class itermonthdates() method

The itermonthdates() method takes two arguments year, month, and returns a generator which contains
DateTime object.

            
year = 2020
month = 01

for date in my_calendar.itermonthdates(year, month):
    print(date)

#PYTHON OUTPUT
2020-01-27
2020-01-28
2020-01-29
2020-01-30
2020-01-31
2020-02-01
2020-02-02
2020-02-03
2020-02-04
2020-02-05
2020-02-06
Using Python Calender Module itermonthdates() method to iterate over dates

Using Python Calendar Module itermonthdates() method to iterate over dates

In the above output, we are getting incorrect dates because some dates belong to the first month, the second month, and others
belong to the third-month march.
To resolve this issue and get dates related to the given month follow the below example.

            
year = 2020
month = 01

for date in my_calendar.itermonthdates(year, month):
    if date.month == month:
        print(date)

#PYTHON OUTPUT
2020-02-01
2020-02-02
2020-02-03
2020-02-04
...
...
...
...
2020-02-26
2020-02-27
2020-02-28
2020-02-29
The method itermonthdates() loops over the dates of a particular month

The method itermonthdates() loops over the dates of a particular month

Now we get dates related to the month Feb.

To get dates in form of a list wrap my_calendar.itermonthdates(year, month) generator inside
list a function and this will return us a list of datetime objects.

            
year = 2020
month = 2
print(list(my_calendar.itermonthdates(year, month)))

#PYTHON OUTPUT
[datetime.date(2020, 1, 27), datetime.date(2020, 1, 28), datetime.date(2020, 1, 29), datetime.date(2020, 1, 30), 
datetime.date(2020, 1, 31), datetime.date(2020, 2, 1), datetime.date(2020, 2, 2), datetime.date(2020, 2, 3), 
....
....
datetime.date(2020, 2, 27), datetime.date(2020, 2, 28), datetime.date(2020, 2, 29), datetime.date(2020, 3, 1)]
Converting dateobjects to list in Python calender module

Converting date objects to list in Python calendar module

Calendar class itermonthdays() method

itermonthdays() takes two arguments year, month, and return generator object for dates. This again can be
converted to a list by using list() a function.

            
year = 2020
month = 2
print(list(my_calendar.itermonthdays(year, month)))

#PYTHON OUTPUT
[0, 0, 0, 0, 0, 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, 0]

Calendar class itermonthdays2() method

itermonthdays2() takes two arguments year, month, and return generator object for date and weekday number
in form of a tuple.

            
year = 2020
month = 2
for date in my_calendar.itermonthdays2(year, month):
    print(date)

#PYTHON OUTPUT
(0, 0)
(0, 1)
(0, 2)
....
....
(27, 3)
(28, 4)
(29, 5)
(0, 6)

Calendar class itermonthdays3() method

itermonthdays3() is similar to the above itermonthdays() and itermonthdays2() methods
but the difference is that this method displays the date, month, and also year in the tuple.

            
year = 2020
month = 2
for date in my_calendar.itermonthdays3(year, month):
    print(date)

#PYTHON OUTPUT
(2020, 1, 27)
(2020, 1, 28)
(2020, 1, 29)
(2020, 1, 30)
(2020, 1, 31)
(2020, 2, 1)
(2020, 2, 2)
....
....
....
(2020, 2, 25)
(2020, 2, 26)
(2020, 2, 27)
(2020, 2, 28)
(2020, 2, 29)
(2020, 3, 1)
The itermonthdays3() method iterates over the dates and returns tuple

The itermonthdays3() method iterates over the dates and returns a tuple

Calendar class itermonthdays4() method

itermonthdays4() returns list where each item is a tuple consisting of four elements representing a year,
month, date, and weekday number.

            
year = 2020
month = 2

for date in my_calendar.itermonthdays4(year, month):
    print(date)

#PYTHON OUTPUT
(2020, 1, 27, 0)
(2020, 1, 28, 1)
(2020, 1, 29, 2)
(2020, 1, 30, 3)
(2020, 1, 31, 4)
(2020, 2, 1, 5)
(2020, 2, 2, 6)
(2020, 2, 3, 0)        
....
....
....
(2020, 2, 25, 1)
(2020, 2, 26, 2)
(2020, 2, 27, 3)
(2020, 2, 28, 4)
(2020, 2, 29, 5)
(2020, 3, 1, 6)
The itermonthdays4() method iterates over the dates and returns tuple with four items

The itermonthdays4() method iterates over the dates and returns tuple with four items

Calendar class iterweekdays() method

iterweekdays() return number of weekday and iterates over weekdays. The first-weekday number can be
specified
using setfirstweekday() method. Which iterates weekdays starting from the first weekday specified.

            
my_calendar.setfirstweekday(3)
for date in my_calendar.iterweekdays():
    print(date)

#PYTHON OUTPUT
3
4
5
6
0
1
2        

Calendar class monthdatescalendar() method

monthdatescalendar() returns multiple lists of date object which contain dates of weeks in a given month.

            
year = 2020
month = 2

for date in my_calendar.monthdatescalendar(year, month):
    print("\n")
    print(date)

#PYTHON OUTPUT
[datetime.date(2020, 1, 27), datetime.date(2020, 1, 28), datetime.date(2020, 1, 29), datetime.date(2020, 1, 30), datetime.date(2020, 1, 31), datetime.date(2020, 2, 1), datetime.date(2020, 2, 2)]
....
....
....        
[datetime.date(2020, 2, 24), datetime.date(2020, 2, 25), datetime.date(2020, 2, 26), datetime.date(2020, 2, 27), datetime.date(2020, 2, 28), datetime.date(2020, 2, 29), datetime.date(2020, 3, 1)]            
The monthdatescalendar() method returns list of datetime object of the year

The monthdatescalendar() method returns list of DateTime object of the year

Calendar class monthdays2calendar() method

monthdays2calendar() returns a list of items where each item is a tuple the first element of that item
represents a day in number form and the second item represents weekday number.

            
year = 2020
month = 2
for date in my_calendar.monthdays2calendar(year, month):
    print("\n")
    print(date)

#PYTHON OUTPUT
[(0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (1, 5), (2, 6)]


[(3, 0), (4, 1), (5, 2), (6, 3), (7, 4), (8, 5), (9, 6)]


[(10, 0), (11, 1), (12, 2), (13, 3), (14, 4), (15, 5), (16, 6)]


[(17, 0), (18, 1), (19, 2), (20, 3), (21, 4), (22, 5), (23, 6)]


[(24, 0), (25, 1), (26, 2), (27, 3), (28, 4), (29, 5), (0, 6)]

Calendar class monthdayscalendar() method

monthdayscalendar() returns a list of items where each item is number representing a day.

            
year = 2020
month = 2
for date in my_calendar.monthdayscalendar(year, month):
    print("\n")
    print(date)

#PYTHON OUTPUT
        
[0, 0, 0, 0, 0, 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, 0]
The monthdayscalendar() method returns days represented by day number

The monthdayscalendar() method returns days represented by day number

Calendar class yeardatescalendar() method

yeardatescalendar() returns the date object for each of the days in that given year. It takes a year as an
argument.

            
year = 2020

for date in my_calendar.yeardatescalendar(year):
    print("\n")
    print(date)

#PYTHON OUTPUT
[[[datetime.date(2019, 12, 30), datetime.date(2019, 12, 31), datetime.date(2020, 1, 1), datetime.date(2020, 1, 2), 
    datetime.date(2020, 1, 3), datetime.date(2020, 1, 4), datetime.date(2020, 1, 5)], [datetime.date(2020, 1, 6),
    ...
    ...
    ...
datetime.date(2020, 1, 7), datetime.date(2020, 1, 8), datetime.date(2020, 1, 9), datetime.date(2020, 1, 10), ]]],

Calendar class yeardays2calendar() method

yeardays2calendar() returns a list of items where each item is a tuple and its first item contains day and
second weekday in number format.

            
year = 2020

for date in my_calendar.yeardays2calendar(year):
    print("\n")
    print(date)
The yeardays2calendar() returns day and weekend in number format

The yeardays2calendar() returns day and weekend in number format

Calendar class yeardayscalendar() method

yeardayscalendar() returns week vise list of all days in a month at a given year.

            
year = 2020
for date in my_calendar.yeardayscalendar(year):
    print("\n")
    print(date)
The yeardayscalendar() return all days in a given month

The yeardayscalendar() return all days in a given month

For more information about the calendar module, you can visit python’s official website by clicking here and know more about this module.

Conclusion

You have reached the end of our post on Demystifying Python Calendar Module with Examples for beginners. We appreciate you for going through this post. If you find anything difficult with our way of
explanation please leave us a comment and if you have enjoyed reading our post then help us grow by sharing this post
link. We Thank You With All Our Heart.

Summary
Review Date
Reviewed Item
Demystifying Python Calendar Module with Examples for beginners
Author Rating
51star1star1star1star1star
Software Name
Python Programming Language
Software Name
Windows Os, Mac Os, Ubuntu Os
Software Category
Programming Langauage