Python calendar module | monthdatescalendar() 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.

monthdatescalendar() method in Python is used to get a list of the weeks in the 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:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to demonstrate working
# of monthdatescalendar() method
  
# importing calendar module
import calendar
  
obj = calendar.Calendar()
  
year = 2018
mnth = 9
  
# priting with monthdatescalendar
print(obj.monthdatescalendar(year, mnth))

chevron_right


Output:

[[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(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)]]

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

Code #2: iterating the list of weeks

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to demonstrate working
# of monthdatescalendar() method
  
# importing calendar module
import calendar
  
obj = calendar.Calendar()
  
# iteratign with monthdatescalendar
for day in obj.monthdatescalendar(2018, 9):
    print(day)

chevron_right


Output:

[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(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)]



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.