monthdays2calendar()

Calendar Module

monthMonth number for which dates are required
yearYear number for which dates are required
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)
Output
[(0, 0), (0, 1), (1, 2), (2, 3), (3, 4), (4, 5), (5, 6)]
[(6, 0), (7, 1), (8, 2), (9, 3), (10, 4), (11, 5), (12, 6)]
[(13, 0), (14, 1), (15, 2), (16, 3), (17, 4), (18, 5), (19, 6)]
[(20, 0), (21, 1), (22, 2), (23, 3), (24, 4), (25, 5), (26, 6)]
[(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 list.
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
Calendar Module in Python itermonthdays() itermonthdays2() itermonthdays3()



Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here





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