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.itermonthdays2(2020,7):
print(x)
Output
(0, 0)
(0, 1)
(1, 2)
(2, 3)
(3, 4)
(4, 5)
(5, 6)
(6, 0)
------
------
(27, 0)
(28, 1)
(29, 2)
(30, 3)
(31, 4)
(0, 5)
(0, 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.itermonthdays2(2020,7):
print(x)
Output
(0, 3)
(0, 4)
(0, 5)
(0, 6)
(0, 0)
(0, 1)
(1, 2)
----
----
----
(30, 3)
(31, 4)
(0, 5)
(0, 6)
(0, 0)
(0, 1)
(0, 2)
As we are getting tuple as output, we can get first element as day number and second element as day of the week.
import calendar
my_cal= calendar.Calendar(firstweekday=3)
for x in my_cal.itermonthdays2(2020,7):
print('day:',x[0],', weekday : ',x[1])
Output
day: 0 , weekday : 3
day: 0 , weekday : 4
day: 0 , weekday : 5
day: 0 , weekday : 6
day: 0 , weekday : 0
day: 0 , weekday : 1
day: 1 , weekday : 2
day: 2 , weekday : 3
---------------------
---------------------
day: 29 , weekday : 2
day: 30 , weekday : 3
day: 31 , weekday : 4
day: 0 , weekday : 5
day: 0 , weekday : 6
day: 0 , weekday : 0
day: 0 , weekday : 1
day: 0 , weekday : 2
Calendar Module in Python itermonthdays() only days of the month
itermonthdays3()
itermonthdays4()
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.