monthdays2calendar() : Returns a list of weeks having day number and week day number as tuple.
Note that days are returned as 0 for the days of the week starting from Previous month and for days of the week extended up to next month. This is our first or 0th element or the first element of the tuple.
Week day number starts from 0 as Monday , 1 as Tuesday and ends at 6 as Sunday.
List of full weeks of the month with each week list having all 7 days tuple with month day number and week day number. The first and last week may contain days of previous month and days of next month as elements.
import calendar
my_cal= calendar.Calendar()
for x in my_cal.monthdays2calendar(2020,7):
print(x)
import calendar
my_cal= calendar.Calendar(firstweekday=0)
for x in my_cal.monthdays2calendar(2020,7):
print(x)
As we are getting a list as output, we can get total number of weeks in a month and number of days in a week. We are using len() here to get number of elements.
import calendar
my_cal= calendar.Calendar(firstweekday=0)
y=my_cal.monthdays2calendar(2020,7)
print("Number of weeks",len(y))
print("Number of days in week",len(y[0]))
Output
Number of weeks 5
Number of days in week 7
As we are getting a list of days as tuple for each week , we can display day and week day number like this.
import calendar
my_cal= calendar.Calendar(firstweekday=0)
for x in my_cal.monthdays2calendar(2020,7):
for y in x:
print("Day :",y[0],", Day of week",y[1])
Output
Day : 0 , Day of week 0
Day : 0 , Day of week 1
Day : 1 , Day of week 2
Day : 2 , Day of week 3
Day : 3 , Day of week 4
Day : 4 , Day of week 5
Day : 5 , Day of week 6
-----------------------
-----------------------
Day : 28 , Day of week 1
Day : 29 , Day of week 2
Day : 30 , Day of week 3
Day : 31 , Day of week 4
Day : 0 , Day of week 5
Day : 0 , Day of week 6