Python calendar module | monthdayscalendar() method
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
import calendar
obj = calendar.Calendar()
year = 2018
month = 9
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
import calendar
obj = calendar.Calendar()
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]
Last Updated :
29 Jul, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...