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.itermonthdays4(2020,7):
print(x)
Output
(2020, 6, 29, 0)
(2020, 6, 30, 1)
(2020, 7, 1, 2)
(2020, 7, 2, 3)
(2020, 7, 3, 4)
(2020, 7, 4, 5)
---------------
---------------
(2020, 7, 26, 6)
(2020, 7, 27, 0)
(2020, 7, 28, 1)
(2020, 7, 29, 2)
(2020, 7, 30, 3)
(2020, 7, 31, 4)
(2020, 8, 1, 5)
(2020, 8, 2, 6)
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.itermonthdays4(2020,7):
print(x)
Output
(2020, 6, 25, 3)
(2020, 6, 26, 4)
(2020, 6, 27, 5)
(2020, 6, 28, 6)
(2020, 6, 29, 0)
(2020, 6, 30, 1)
(2020, 7, 1, 2)
(2020, 7, 2, 3)
(2020, 7, 3, 4)
---------------
---------------
(2020, 7, 30, 3)
(2020, 7, 31, 4)
(2020, 8, 1, 5)
(2020, 8, 2, 6)
(2020, 8, 3, 0)
(2020, 8, 4, 1)
(2020, 8, 5, 2)
As we are getting tuple as output, we can get first element as year , 2nd as month and 3rd as day the month. 4th element is week day in number
import calendar
my_cal= calendar.Calendar(firstweekday=3)
for x in my_cal.itermonthdays4(2020,7):
print('Year:',x[0],', Month : ',x[1],', Day : ',x[2],', Week Day : ',x[3])
Output
Year: 2020 , Month : 6 , Day : 25 Week Day : 3
Year: 2020 , Month : 6 , Day : 26 Week Day : 4
Year: 2020 , Month : 6 , Day : 27 Week Day : 5
Year: 2020 , Month : 6 , Day : 28 Week Day : 6
Year: 2020 , Month : 6 , Day : 29 Week Day : 0
Year: 2020 , Month : 6 , Day : 30 Week Day : 1
Year: 2020 , Month : 7 , Day : 1 Week Day : 2
-------------------------------------------------
-------------------------------------------------
-------------------------------------------------
Year: 2020 , Month : 7 , Day : 29 Week Day : 2
Year: 2020 , Month : 7 , Day : 30 Week Day : 3
Year: 2020 , Month : 7 , Day : 31 Week Day : 4
Year: 2020 , Month : 8 , Day : 1 Week Day : 5
Year: 2020 , Month : 8 , Day : 2 Week Day : 6
Year: 2020 , Month : 8 , Day : 3 Week Day : 0
Year: 2020 , Month : 8 , Day : 4 Week Day : 1
Year: 2020 , Month : 8 , Day : 5 Week Day : 2
Calendar Module in Python itermonthdays()
itermonthdays2()
itermonthdays3()
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.