monthdayscalendar()

Calendar Module

monthMonth number for which dates are requried
yearYear number for which dates are requried
monthdayscalendar() : Returns a list of full weeks having day number .

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.

import calendar
my_cal= calendar.Calendar()
for x in my_cal.monthdayscalendar(2020,7):
	print(x)
Output
[0, 0, 1, 2, 3, 4, 5]
[6, 7, 8, 9, 10, 11, 12]
[13, 14, 15, 16, 17, 18, 19]
[20, 21, 22, 23, 24, 25, 26]
[27, 28, 29, 30, 31, 0, 0]
By updating the first day of the week we can get different elements of the list.
import calendar
my_cal= calendar.Calendar(firstweekday=3)
for x in my_cal.monthdayscalendar(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 ( items ) .
import calendar
my_cal= calendar.Calendar(firstweekday=0)
y=my_cal.monthdayscalendar(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.monthdayscalendar(2020,7):
    for y in x:
        print("Day :",y)
Output
Day : 0
Day : 0
Day : 1
Day : 2
Day : 3
Day : 4
Day : 5
Day : 6
--------
--------
Day : 26
Day : 27
Day : 28
Day : 29
Day : 30
Day : 31
Day : 0
Day : 0
Calendar Module in Python itermonthdays() itermonthdays2() itermonthdays3()




plus2net.com



Post your comments , suggestion , error , requirements etc here




We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2020 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer