Implementing ISO 8601 week dates
An ISO week number needs two values: a week index and a week-year. The week-year can differ from the Gregorian year of the date.
Three rules define an ISO week
- Weeks start on Monday and contain seven days.
- ISO week 1 is the week that contains January 4 (equivalently, the year’s first Thursday).
- The ISO week-year is the Gregorian year of the Thursday in that week.
These rules explain why January 1, 2027 is 2026-W53. The week begins Monday, December 28, 2026; its Thursday is December 31, 2026. January 4, 2027 is the next Monday and begins 2027-W01. Verify the dates interactively with the ISO week visualizer.
A UTC civil-date algorithm
Represent a date as an integer year, zero-based month, and day. Convert to a UTC calendar date, move to that date’s Thursday, and use the Monday containing January 4 as the first week boundary. The week number is the whole number of seven-day steps from that Monday plus one.
const isoWeekday = weekday === 0 ? 7 : weekday;
const thursday = addDays(date, 4 - isoWeekday);
const isoWeekYear = thursday.year;
const firstMonday = mondayOf({ year: isoWeekYear, month: 0, day: 4 });
const week = 1 + Math.floor(daysBetween(firstMonday, mondayOf(date)) / 7);Using civil-date operations avoids local-time offsets. In JavaScript, a date-only string is interpreted differently by APIs depending on whether it is parsed as UTC or local time. Avoid round-tripping calendar dates through local midnight, where daylight-saving transitions can make elapsed-hour arithmetic differ from civil-day arithmetic.
How many weeks are in an ISO year?
An ISO year has 52 or 53 weeks. The week number of December 28 is always the final ISO week number of its week-year, so an implementation can use that date to compute the year’s length. Calendar Lab uses this property for the live visualizer and tests both year boundaries.
Related tools and notes
Open the calendar grid explorer, compare dates with the date difference calculator, or read about Gregorian date arithmetic.