Python calendar monthdatescalendar tutorial

Introduction :

monthdatescalendar method of python module is used to get a list of weeks of a month of a specific year as full weeks. Each day of a week is represented as datetime.date object.

Syntax of monthdatescalendar :

The syntax of this method is as below :

monthdatescalendar(year, month)

Here, year and month are year and month for the calendar we want the list.

Example of monthdatescalendar :

Let’s take a look at the below example program :

import calendar

cal = calendar.Calendar()

print(cal.monthdatescalendar(2020, 7))

It will print :

[[datetime.date(2020, 6, 29), datetime.date(2020, 6, 30), datetime.date(2020, 7, 1), datetime.date(2020, 7, 2), datetime.date(2020, 7, 3), datetime.date(2020, 7, 4), datetime.date(2020, 7, 5)], [datetime.date(2020, 7, 6), datetime.date(2020, 7, 7), datetime.date(2020, 7, 8), datetime.date(2020, 7, 9), datetime.date(2020, 7, 10), datetime.date(2020, 7, 11), datetime.date(2020, 7, 12)], [datetime.date(2020, 7, 13), datetime.date(2020, 7, 14), datetime.date(2020, 7, 15), datetime.date(2020, 7, 16), datetime.date(2020, 7, 17), datetime.date(2020, 7, 18), datetime.date(2020, 7, 19)], [datetime.date(2020, 7, 20), datetime.date(2020, 7, 21), datetime.date(2020, 7, 22), datetime.date(2020, 7, 23), datetime.date(2020, 7, 24), datetime.date(2020, 7, 25), datetime.date(2020, 7, 26)], [datetime.date(2020, 7, 27), datetime.date(2020, 7, 28), datetime.date(2020, 7, 29), datetime.date(2020, 7, 30), datetime.date(2020, 7, 31), datetime.date(2020, 8, 1), datetime.date(2020, 8, 2)]]

We called this method for July, 2020 but as you have seen, it returns all dates starting from 29th June to 2nd August. This is because, it will return all dates for a month including other days for the start and end weeks. 29th June is Monday for the first week of July and 2nd August is Sunday for the last week of July.

Another example for October 2020 :

import calendar

cal = calendar.Calendar()

print(cal.monthdatescalendar(2020, 10))

Output :

[[datetime.date(2020, 9, 28), datetime.date(2020, 9, 29), datetime.date(2020, 9, 30), datetime.date(2020, 10, 1), datetime.date(2020, 10, 2), datetime.date(2020, 10, 3), datetime.date(2020, 10, 4)], [datetime.date(2020, 10, 5), datetime.date(2020, 10, 6), datetime.date(2020, 10, 7), datetime.date(2020, 10, 8), datetime.date(2020, 10, 9), datetime.date(2020, 10, 10), datetime.date(2020, 10, 11)], [datetime.date(2020, 10, 12), datetime.date(2020, 10, 13), datetime.date(2020, 10, 14), datetime.date(2020, 10, 15), datetime.date(2020, 10, 16), datetime.date(2020, 10, 17), datetime.date(2020, 10, 18)], [datetime.date(2020, 10, 19), datetime.date(2020, 10, 20), datetime.date(2020, 10, 21), datetime.date(2020, 10, 22), datetime.date(2020, 10, 23), datetime.date(2020, 10, 24), datetime.date(2020, 10, 25)], [datetime.date(2020, 10, 26), datetime.date(2020, 10, 27), datetime.date(2020, 10, 28), datetime.date(2020, 10, 29), datetime.date(2020, 10, 30), datetime.date(2020, 10, 31), datetime.date(2020, 11, 1)]]

python montdatescalendar