month | Month number for which dates are required |
year | Year number for which dates are required |
import calendar
my_cal= calendar.Calendar()
for x in my_cal.itermonthdays3(2020,7):
print(x)
Output
(2020, 6, 29)
(2020, 6, 30)
(2020, 7, 1)
(2020, 7, 2)
(2020, 7, 3)
(2020, 7, 4)
(2020, 7, 5)
(2020, 7, 6)
-------------
-------------
(2020, 7, 27)
(2020, 7, 28)
(2020, 7, 29)
(2020, 7, 30)
(2020, 7, 31)
(2020, 8, 1)
(2020, 8, 2)
By updating the first day of the week we can get different elements of the iterator.
import calendar
my_cal= calendar.Calendar(firstweekday=3)
for x in my_cal.itermonthdays3(2020,7):
print(x)
Output
(2020, 6, 25)
(2020, 6, 26)
(2020, 6, 27)
(2020, 6, 28)
(2020, 6, 29)
(2020, 6, 30)
(2020, 7, 1)
(2020, 7, 2)
(2020, 7, 3)
(2020, 7, 4)
------------
------------
(2020, 7, 30)
(2020, 7, 31)
(2020, 8, 1)
(2020, 8, 2)
(2020, 8, 3)
(2020, 8, 4)
(2020, 8, 5)
As we are getting tuple as output, we can get first element ( item ) as year , 2nd as month and 3rd as day the month.
import calendar
my_cal= calendar.Calendar(firstweekday=3)
for x in my_cal.itermonthdays3(2020,7):
print('Year:',x[0],', Month : ',x[1],', Day : ',x[2])
Output
Year: 2020 , Month : 6 , Day : 25
Year: 2020 , Month : 6 , Day : 26
Year: 2020 , Month : 6 , Day : 27
Year: 2020 , Month : 6 , Day : 28
Year: 2020 , Month : 6 , Day : 29
Year: 2020 , Month : 6 , Day : 30
Year: 2020 , Month : 7 , Day : 1
Year: 2020 , Month : 7 , Day : 2
Year: 2020 , Month : 7 , Day : 3
Year: 2020 , Month : 7 , Day : 4
Year: 2020 , Month : 7 , Day : 5
----------------------------------
----------------------------------
Year: 2020 , Month : 7 , Day : 29
Year: 2020 , Month : 7 , Day : 30
Year: 2020 , Month : 7 , Day : 31
Year: 2020 , Month : 8 , Day : 1
Year: 2020 , Month : 8 , Day : 2
Year: 2020 , Month : 8 , Day : 3
Year: 2020 , Month : 8 , Day : 4
Year: 2020 , Month : 8 , Day : 5
Calendar Module in Python itermonthdays() only days of the month
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.