monthdatescalendar() : Returns an list of weeks having date object of full week.
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 date objects. 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.monthdatescalendar(2020,7):
print(x)
import calendar
my_cal= calendar.Calendar(firstweekday=3)
for x in my_cal.monthdatescalendar(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.monthdatescalendar(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 date objects for each week , we can display month, year and day like this.
print("Day number of 4th element of first week: day,month,year \n",
y[0][4].day , y[0][4].month,y[0][4].year)
Output
Day number of 4th element of first week: day,month,year
3 7 2020