โญ Support Time.now: Join our Premium Plan for an ad-free experience! โญ Support Us: Go Premium Ad-Free!

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

  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+.

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.