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 & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.