Week numbers in Python

How to get the week number from a date

To get the ISO week number (1-53), use d.isocalendar()[1].

The variable d is a date or datetime object.

To get the corresponding four-digit year (e.g. 2022), use d.isocalendar()[0].

Read more about date.isocalendar() in the Python manual.

How to get the number of weeks in a year

To get the number of ISO weeks (i.e. the number of the last week) in a year, use date(year, 12, 28).isocalendar()[1].

year is an integer four-digit year.