yeardayscalendar()

yearYear number for which dates are requried
widthOptional , default =3 , list of months in a row
yeardayscalendar() : Returns a list of months with full week with day numbers of the month.

Return : List of months where months have weeks as elements. Each week have 7 day numbers of the month. Week list are formed based on weekday number

Weekday number : 0 to 6 : , 0 is Monday ,1 is Tuesday, 6 is Sunday ( based on setting of firstweekday() )
import calendar
my_cal= calendar.Calendar()
for x in my_cal.yeardayscalendar(2020):
	print(x)
The output is list of rows of months with full weeks and day number as items. Based on the setting of firstweekday(), the first week of the year will start from previous year, last week and will extend up to next year’s first week.
[[[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]],
 [[0, 0, 0, 0, 0, 1, 2], [3, 4, 5, 6, 7, 8, 9],
-------------
-------------
-------------
 [[0, 0, 0, 0, 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, 0, 0, 0, 0, 0, 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, 0]]]

Details of output

List of month rows ( Number of months in a row is specified by width number )
Each Month contains list of full weeks ( 4 to 6 weeks in a month )
Each week contains 7 monthly day numbers ( items )

You can check the output by removing the comment of different lines.
import calendar
my_cal= calendar.Calendar()
y= my_cal.yeardayscalendar(2020,2) 
#print(y[5]) # list of month rows , it has 2 months 
#print(y[5][1]) # list of full weeks of 2nd month of 6th row
#print(y[5][1][3]) # list of days of fourth  week

## 5th  item of the fourth week of 2nd month of 6th row
#print(y[5][1][3][5])  # 26
print("Day :",y[5][1][3][5])
Output ( last line only )
Day : 26 
We are getting output as list. So length or number of elements ( items ) present in the list ( iterable object) we can get by using len() function.
import calendar
my_cal= calendar.Calendar()
y= my_cal.yeardayscalendar(2020,2)
print("No. of rows of months  : ", len(y))               # 6
print("No. of months in the row : ", len(y[1]))          # 2
print("No. of full weeks in first month :",len(y[0][0])) # 5
print("No. of days in first month, first week:",len(y[0][0][0]))
Output
No. of rows of months  :  6
No. of months in the row :  2
No. of full weeks in first month : 5
No. of days in first month, first week: 7
In above code the line y= my_cal.yeardayscalendar(2020,2) set the width to 2.

With width=2 we will get 6 elements or items having 2 months each. ( 12 month in a year ) . So the output of first line len(y) is 6

We are getting two months in a row or each element ( item ). So the output of second line len(y[1]) is 2

The first item of first row is the month, so the the len function len(y[0][0]) returns the number of full weeks in the month as 5.

We know each week has 7 days. So the value len(y[0][0][0]) returns 7.

Day number

We are getting list with day numbers of month as items so we can display day number of the month.
print("Day :",y[5][1][3][5]) 
Output
Day : 26 
Calendar Module in Python itermonthdays() itermonthdays2() itermonthdays3()



Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com







    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