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. 2013), 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.
Week numbers in …
A lot of software that deal with dates has poor support for week number, in particular the ISO week numbering scheme that is used on this site.
The concept of week numbers is virtually unknown in large parts of the world, so adding support for them is not high on the agenda for most software companies.
However, with a few tricks it is often possible to make week numbers calculations, even if the application does not have native support for week numbers.
These pages explain how to use week numbers in various popular software applications and programming languages.