Python - yeardatescalendar()


Python tip:

You can get a list of all months, containing all the dates for a year with the yeardatescalendar method.

You need to provide the date and width as arguments.

The return looks like this:

[list of months[month[week[datetime object]]]]
import calendar

cal = calendar.Calendar()
year = cal.yeardatescalendar(2022, 6)

for month in year:
    print(month)

"""
Results:

[
    [
        [
            datetime.date(2021, 12, 27),
            datetime.date(2021, 12, 28),
            datetime.date(2021, 12, 29),
            datetime.date(2021, 12, 30),
            datetime.date(2021, 12, 31),
            datetime.date(2022, 1, 1),
            datetime.date(2022, 1, 2),
        ],
        [
            datetime.date(2022, 1, 3),
            datetime.date(2022, 1, 4),
            datetime.date(2022, 1, 5),
            datetime.date(2022, 1, 6),
            datetime.date(2022, 1, 7),
            datetime.date(2022, 1, 8),
            datetime.date(2022, 1, 9),
        ],

        ...
    ],
]
"""