Python Program for calendar itermonthdates() Method with Examples

Calendar Module:

The calendar module allows you to output calendars like a program and includes extra calendar-related operations. Calendar module functions and classes make use of an idealized calendar, the current Gregorian calendar extended in both directions indefinitely.

itermonthdates() Method:

Itermonthdates() returns an iterator for the specified month (1-12) of the year.
This iterator will return all days (as datetime.date objects) for the month, as well as all days before or after the start or end of the month that are required to complete a week.

Syntax:

itermonthdates(year, month)

Note:

datetime.date: A date object represents a date (year, month, and day) in an idealized calendar, which is the existing Gregorian calendar stretched in both directions indefinitely. January 1 of year 1 is referred to as day number 1, January 2 of year 1 is referred to as day number 2, and so on.

Parameter Values:

year: This is required. It is a number. The year for which the calendar should be created.

month: This is required. It is a number. The month for which the calendar should be created.

Return Value:

Returns an iterator for the specified month (1-12) of the year.

Program for calendar itermonthdates() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Example1:

Approach:

  • Import calendar module using the import keyword.
  • Give the year as static input and store it in a variable.
  • Give the month as static input and store it in another variable.
  • Call the Calendar() function and store it in another variable.
  • Apply itermonthdates() method to the above calendar by passing the given year, month as the arguments and store it in another variable.
  • Iterate in the above result using the for loop.
  • Inside the loop, print the iterator value.
  • The Exit of the Program.

Below is the implementation:

# Import calendar module using the import keyword.
import calendar
# Give the year as static input and store it in a variable.
gvn_yr = 2020
# Give the month as static input and store it in another variable.
gvn_mont = 3
# Call the Calendar() function and store it in another variable.
calendr = calendar.Calendar()
# Apply itermonthdates() method to the above calendar by passing the given year,
# month as the arguments and store it in another variable.
rslt = calendr.itermonthdates(gvn_yr, gvn_mont)
# Iterate in the above result using the for loop.
for itr in rslt:
    # Inside the loop, print the iterator value.
    print(itr)

Output:

2020-02-24
2020-02-25
2020-02-26
2020-02-27
2020-02-28
2020-02-29
2020-03-01
2020-03-02
2020-03-03
2020-03-04
2020-03-05
2020-03-06
2020-03-07
2020-03-08
2020-03-09
2020-03-10
2020-03-11
2020-03-12
2020-03-13
2020-03-14
2020-03-15
2020-03-16
2020-03-17
2020-03-18
2020-03-19
2020-03-20
2020-03-21
2020-03-22
2020-03-23
2020-03-24
2020-03-25
2020-03-26
2020-03-27
2020-03-28
2020-03-29
2020-03-30
2020-03-31
2020-04-01
2020-04-02
2020-04-03
2020-04-04
2020-04-05

Example2:

Approach:

  • Import calendar module using the import keyword.
  • Give the year as static input and store it in a variable.
  • Give the month as static input and store it in another variable.
  • Call the Calendar() function by setting firstweekday=2 and store it in another variable.
  • Apply itermonthdates() method to the above calendar by passing the given year, month as the arguments and store it in another variable.
  • Iterate in the above result using the for loop.
  • Inside the loop, print the iterator value.
  • The Exit of the Program.

Below is the implementation:

# Import calendar module using the import keyword.
import calendar
# Give the year as static input and store it in a variable.
gvn_yr = 2012
# Give the month as static input and store it in another variable.
gvn_mont = 5
# Call the Calendar() function by setting firstweekday=2 and store it in
# another variable.
calendr = calendar.Calendar(firstweekday=2)
# Apply itermonthdates() method to the above calendar by passing the given year,
# month as the arguments and store it in another variable.
rslt = calendr.itermonthdates(gvn_yr, gvn_mont)
# Iterate in the above result using the for loop.
for itr in rslt:
    # Inside the loop, print the iterator value.
    print(itr)

Output:

2012-04-25
2012-04-26
2012-04-27
2012-04-28
2012-04-29
2012-04-30
2012-05-01
2012-05-02
2012-05-03
2012-05-04
2012-05-05
2012-05-06
2012-05-07
2012-05-08
2012-05-09
2012-05-10
2012-05-11
2012-05-12
2012-05-13
2012-05-14
2012-05-15
2012-05-16
2012-05-17
2012-05-18
2012-05-19
2012-05-20
2012-05-21
2012-05-22
2012-05-23
2012-05-24
2012-05-25
2012-05-26
2012-05-27
2012-05-28
2012-05-29
2012-05-30
2012-05-31
2012-06-01
2012-06-02
2012-06-03
2012-06-04
2012-06-05

Method #2: Using Built-in Functions (User Input)

Example1:

Approach:

  • Import calendar module using the import keyword.
  • Give the year as user input using the int(input()) function and store it in a variable.
  • Give the month as user input using the int(input()) function and store it in another variable.
  • Call the Calendar() function and store it in another variable.
  • Apply itermonthdates() method to the above calendar by passing the given year, month as the arguments and store it in another variable.
  • Iterate in the above result using the for loop.
  • Inside the loop, print the iterator value.
  • The Exit of the Program.

Below is the implementation:

# Import calendar module using the import keyword.
import calendar
# Give the year as user input using the int(input()) function and store it in a variable.
gvn_yr = int(input("Enter some random year = "))
# Give the month as user input using the int(input()) function and store it in another variable.
gvn_mont = int(input("Enter some random month = "))
# Call the Calendar() function and store it in another variable.
calendr = calendar.Calendar()
# Apply itermonthdates() method to the above calendar by passing the given year,
# month as the arguments and store it in another variable.
rslt = calendr.itermonthdates(gvn_yr, gvn_mont)
# Iterate in the above result using the for loop.
for itr in rslt:
    # Inside the loop, print the iterator value.
    print(itr)

Output:

Enter some random year = 2018
Enter some random month = 6
2018-05-28
2018-05-29
2018-05-30
2018-05-31
2018-06-01
2018-06-02
2018-06-03
2018-06-04
2018-06-05
2018-06-06
2018-06-07
2018-06-08
2018-06-09
2018-06-10
2018-06-11
2018-06-12
2018-06-13
2018-06-14
2018-06-15
2018-06-16
2018-06-17
2018-06-18
2018-06-19
2018-06-20
2018-06-21
2018-06-22
2018-06-23
2018-06-24
2018-06-25
2018-06-26
2018-06-27
2018-06-28
2018-06-29
2018-06-30
2018-07-01