Python - monthdays2calendar()


Python tip:

To get a list of the weeks in a certain month, INCLUDING weekday numbers, you can use the monthdays2calendar method.

You need to provide the date and month as arguments.

A list of lists, each containing seven tuples of day numbers and weekday numbers, is returned.

import calendar

cal = calendar.Calendar()
weeks = cal.monthdays2calendar(2022, 7)

print(weeks)

"""
Results:

[
    [(0, 0), (0, 1), (0, 2), (0, 3), (1, 4), (2, 5), (3, 6)],
    [(4, 0), (5, 1), (6, 2), (7, 3), (8, 4), (9, 5), (10, 6)],
    [(11, 0), (12, 1), (13, 2), (14, 3), (15, 4), (16, 5), (17, 6)],
    [(18, 0), (19, 1), (20, 2), (21, 3), (22, 4), (23, 5), (24, 6)],
    [(25, 0), (26, 1), (27, 2), (28, 3), (29, 4), (30, 5), (31, 6)],
]
"""