Python Calendar Module | itermonthdates () method

Python Methods and Functions

itermonthdates() returns an iterator for the month (1-12) of the year. This iterator will return all days of the month and all days before or after the start of the month that it takes to get a full week.

  Syntax:  itermonthdates (year, month)  Parameter :   year:  year of the calendar  month:  month of the calendar  Returns:  an iterator for the month. 

Code # 1:

# Python demonstration program
# of the itermonthdates () method

 
# importing the calendar module

from calendar import Calendar

 

obj = calendar.Calendar ()

 
# iteratign with itermonthdates

for day in obj.itermonthdates ( 2018 , 9 ):

print (day)

Exit:

 2018-08-27 2018-08-28 2018-08-29 2018-08- 30 2018-08-31. ... ... 2018-09-26 2018-09-27 2018-09-28 2018-09-29 2018-09-30 

Code # 2:

# Python demo program
# itermonthdates () method

 
# import calendar module

import calendar

 
# use with firstweekday = 5

obj = calendar. Calendar (firstweekday = 5 )

 
# iteratign with itermonthdates

for day in obj.itermonthdates ( 2018 , 4 ):

print (day)

Exit:

 2018-08-30 2018-08-31 2018-09-01 2018-09-02 2018-09-03. ... 2018-09-30 2018-10-01 2018-10-02 2018-10-03 2018-09-01 2018-09-02 2018-09-03. ... 2018-09-28. ... 0018-03-31 0018-04-01 0018-04-02. ... 2018-04-28 2018-04-29 2018-04-30 2018-05-01 2018-05-02 




Get Solution for free from DataCamp guru