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.date objects.

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 module
from calendar import Calendar
obj = Calendar()
# iterating with itermonthdates
monthIterator = obj.itermonthdates(2021, 1)
# printing days in month
for 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 the monthIterator variable.
  • Line 8: We use the for loop to extract days in the monthIterator iterator. Then, we print each date on the console.

Free Resources