Python calendar yeardatescalendar method explanation with examples

Introduction :

yeardatescalendar method of python calendar module is used to get all dates for a specific year. It returns one list of rows of months. It takes one width parameter, that is used to define how many numbers of months each row should contain. Each month contains weeks between 4 to 6. Each week contains 1 to 7 days. Each day is a datetime.date object.

Definition of the method :

This method is defined as below :

yeardatescalendar(year, width=3)

Here, year is the year to find the dates and width is the number of months each month row should contain. The default value is 3 i.e. it returns four rows for a year by default.

Example :

Let’s take a look at the below example :

import calendar

cal = calendar.Calendar()

print(cal.yeardatescalendar(2020,10))

It will give one output something below :

[[[[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), datetime.date(2020, 1, 11), datetime.date(2020, 1, 12)], [datetime.date(2020, 1, 13), datetime.date(2020, 1, 14), datetime.date(2020, 1, 15), datetime.date(2020, 1, 16), datetime.date(2020, 1, 17), datetime.date(2020, 1, 18), datetime.date(2020, 1, 19)], [datetime.date(2020, 1, 20), datetime.date(2020, 1, 21), datetime.date(2020, 1, 22), datetime.date(2020, 1, 23),........, [datetime.date(2020, 12, 28), datetime.date(2020, 12, 29), datetime.date(2020, 12, 30), datetime.date(2020, 12, 31), datetime.date(2021, 1, 1), datetime.date(2021, 1, 2), datetime.date(2021, 1, 3)]]]]