Soutenez Time.now : Rejoignez notre Plan Premium pour une expérience sans publicité ! Soutenez-nous : Passez Premium sans pub !

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.

Instructions étape par étape

  1. Import the datetime module.
  2. Call datetime.date.today().isocalendar() which returns (year, week, weekday).
  3. Take index [1] for the week number, or use the .week attribute on Python 3.9+.

Bon à savoir

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.