Python Calendar Module | monthdatescalendar () method

Python Methods and Functions

monthdatescalendar() is used in Python to get a list of weeks in a month of the year as full weeks.

  Syntax:  monthdatescalendar (year, month)  Parameter:   year:  year of the calendar  month:  month of the calendar  Returns:  a list of the weeks in the month. 

Code # 1:

# Python program for demonstrations
# monthdatescalendar () method

 
# import calendar module

import calendar

 

obj = calendar. Calendar ()

 

year = 2018

mnth = 9

 
# print with month of date

print (obj.monthdatescalendar (year, mnth))

Exit:

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

Note that the weeks in the output are lists of seven datetime.date objects.

Code # 2: repeating list of weeks

# Python demonstration program
# monthdatescalendar () method

  
# importing the calendar module

import calendar

 

obj = calendar.Calendar ()

 
# iteratign with monthdatescalendar

for day in obj.monthdatescalendar ( 2018 , 9 ):

print (day)

Exit:

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





Get Solution for free from DataCamp guru