Python calendar module | monthdayscalendar() method (original) (raw)

Last Updated : 29 Jul, 2021

Calendar module allows to output calendars like program, and provides additional useful functions related to the calendar. Functions and classes defined in Calendar module use an idealized calendar, the current Gregorian calendar extended indefinitely in both directions.
monthdayscalendar() method in Python is used to get a list of the weeks in the month of the year as full weeks.

Syntax: monthdayscalendar(year, month)

Parameter: year: year of the calendar month: month of the calendar

Returns: a list of the weeks in the month.

Code #1:

Python3 `

Python program to demonstrate working

of monthdayscalendar() method

importing calendar module

import calendar

obj = calendar.Calendar()

year = 2018 month = 9

printing with monthdayscalendar

print(obj.monthdayscalendar(year, month))

`

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, 30]]

Note that weeks in the output are lists of seven day numbers.

Code #2: iterating the list of weeks

Python3 `

Python program to demonstrate working

of monthdayscalendar() method

importing calendar module

import calendar

obj = calendar.Calendar()

iterating with monthdayscalendar

for day in obj.monthdayscalendar(2018, 9): print(day)

`

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, 30]