Functional Weave
Code in TypeScript

dates.week-number@1.0.0

README.md

1,020 bytes · view raw

# dates.week-number

The ISO 8601 week of a date. Weeks run Monday to Sunday, and week 1 of a year
is the week containing that year's first Thursday (equivalently, the week
containing 4 January). A year has 52 or 53 weeks.

The trap is the year. Around 1 January a date can belong to the previous or
the next ISO week-year: 1 January 2027 is in week 53 of 2026, and 30 December
2024 is in week 1 of 2025. So the result carries `weekYear` alongside `week`,
and `label` ("2026-W53") is built from the week-year, never the calendar year.
A report keyed on calendar year and ISO week puts 30 December 2024 in "2024
week 1", eleven months early.

This is the ISO scheme only. US-style week numbers (weeks starting Sunday,
week 1 containing 1 January) and "week of the month" are different questions.

The week is found from the Thursday of the date's week, whose calendar year is
by definition the week-year, using the dates.add-days kernel and
dates.day-of-week, so it is exact for every date from 0001-01-01 to 9999-12-31.