Python Calendar itermonthdates() Method with Example

Python Calendar.itermonthdates() Method: In this tutorial, we will learn about the itermonthdates() method of Calendar class in Python with its usage, syntax, and examples. By Hritika Rajput Last updated : April 24, 2023

Python Calendar.itermonthdates() Method

The Calendar.itermonthdates() method is an inbuilt method of the Calendar class of calendar module, it returns an iterator for the given month (1-12) in the given year. The iterator will return all days (as datetime.date objects) for the month and all days before the start of the month or after the end of the month that are required to get a complete week.

Module

The following module is required to use itermonthdates() method:

import calendar

Class

The following class is required to use itermonthdates() method:

from calendar import Calendar

Syntax

The following is the syntax of itermonthdates() method:

itermonthdates(year, month)

Parameter(s)

The following are the parameter(s):

  • year: represents the year of the calendar
  • month: represents month of the calendar

Return Value

The function returns an iterator for the given month of the given year.

Example of Calendar.itermonthdates() Method in Python

# Python program to illustrate the 
# use of itermonthdates() method

# import class
import calendar

# Creating Calendar Instance
cal = calendar.Calendar()
year = 2020
month = 2
# Complete weeks will be printed
for i in cal.itermonthdates(year, month):
    print(i)
print()

# Setting firstweekday value to 3
cal = calendar.Calendar(firstweekday = 3)
year = 2016
month = 2
for i in cal.itermonthdates(year, month):
    print(i)

Output

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

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

Reference: calendar.Calendar.itermonthdates





Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.