Week numbers in Python
To get the current ISO 8601 week number in Python, use the following code snippet:
import datetime
# Returns ISO week number
datetime.date.today().isocalendar()[1]
* Ensure your system or application regional settings are set to follow ISO 8601 standards (weeks start on Monday) if the output varies.
Step-by-Step Instructions
- Import the datetime module.
- Call datetime.date.today().isocalendar() which returns (year, week, weekday).
- Take index [1] for the week number, or use the .week attribute on Python 3.9+.
Good to Know
isocalendar() also returns the ISO year, which differs from the calendar year around New Year (December 29 2026 belongs to ISO year 2027, week 1 in some years). Use the returned year, not date.year, when labelling weeks.