What is the calendar.itermonthdates() method in Python?
This itermonthdates() method from the calendar module extracts the days of a month iterator. It returns an iterator of the month (January to December) which allows extracting days before and after a month to complete a week.
Month iterator returns days as
datetime.dateobjects.
Syntax
itermonthdates(year, month)
Parameters
It takes the following two parameters:
year: The year of the calendar.month: The month of the calendar.
Return value
It returns an iterator for month.
Explanation
Now, let’s discuss the itermonthdates() method with examples:
# Example#1# loading Calender class from calender modulefrom calendar import Calendarobj = Calendar()# iterating with itermonthdatesmonthIterator = obj.itermonthdates(2021, 1)# printing days in monthfor day in monthIterator:print(day)
- Line 4: We instantiate the calendar class from the Python calendar module.
- Line 6: We invoke the
itermonthdates()function with year=2021 and month=1 to extract the month iterator in themonthIteratorvariable. - Line 8: We use the
forloop to extract days in themonthIteratoriterator. Then, we print each date on the console.