Method Deck / dates

ISO week number

Computes the ISO 8601 week-numbering year and week number for a given date.

Published for review. The Founder is checking every card himself; anything he strikes comes down the same hour.

Try it

Inputs

Outputs

Steps

  1. Parse the date as a proleptic Gregorian civil date and find its ISO weekday (Monday=1 .. Sunday=7) from the day-number modulo 7 (Hinnant days_from_civil, cited above), calibrated against 1970-01-01 being a Thursday.
  2. Find the Thursday of the same ISO week: subtract (iso_weekday - 4) days from the date's day-number.
  3. The ISO week-numbering year is the calendar year that Thursday falls in.
  4. iso_week = floor((Thursday's day-number - day-number of January 1 of the ISO week-numbering year) / 7) + 1.

Constants

None.

Sources

Worked example and test vectors

NameInputsAnswer
worked example{"date":"2024-01-01"}2024-W01
test vector: date belongs to prior ISO year{"date":"2016-01-01"}2015-W53
test vector: year with 53 ISO weeks{"date":"2020-12-31"}2020-W53

Published card