ISO Week Number and Week Start Date
Calculate ISO 8601 week numbers and convert a year plus week back to the Monday that starts it, with round-trip validation.
Code
export function isoWeek(date = new Date()) {
const value = toUtcDate(date);
const thursday = new Date(value.getTime());
const weekday = value.getUTCDay() || 7;
thursday.setUTCDate(value.getUTCDate() + 4 - weekday);
const year = thursday.getUTCFullYear();
const firstThursday = new Date(Date.UTC(year, 0, 4));
const offset = firstThursday.getUTCDay() || 7;
firstThursday.setUTCDate(firstThursday.getUTCDate() + 4 - offset);
const week = 1 + Math.round((thursday - firstThursday) / (7 * 24 * 60 * 60 * 1000));
return { year, week };
}
export function isoWeekStart(year, week) {
if (!Number.isInteger(year) || year < 1 || year > 9999) throw new RangeError("year out of range");
if (!Number.isInteger(week) || week < 1 || week > 53) throw new RangeError("week out of range");
const januaryFourth = new Date(Date.UTC(year, 0, 4));
const weekday = januaryFourth.getUTCDay() || 7;
const monday = new Date(januaryFourth.getTime());
monday.setUTCDate(januaryFourth.getUTCDate() - weekday + 1 + (week - 1) * 7);
const check = isoWeek(monday);
if (check.year !== year || check.week !== week) {
throw new RangeError("week does not exist in that ISO year");
}
return monday;
}
function toUtcDate(date) {
if (!(date instanceof Date) || Number.isNaN(date.getTime())) {
throw new TypeError("date must be a valid Date");
}
return new Date(Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate()));
}
const week = isoWeek(new Date("2026-01-01T00:00:00Z"));
console.log(week);
console.log(isoWeekStart(week.year, week.week).toISOString().slice(0, 10));
console.log(isoWeek(new Date("2024-12-30T00:00:00Z")));Output
Captured from a sandboxed run of this snippet in the build pipeline.
{ year: 2026, week: 1 }
2025-12-29
{ year: 2025, week: 1 }How it works
Count the functions isoWeek, isoWeekStart and toUtcDate as the entire surface area of ISO Week Number and Week Start Date.
Invalid states are rejected up front: it throws RangeError and TypeError instead of continuing with bad data.
The result comes back as an object carrying year.
What to watch for
- The guard
check.year !== year || check.week !== weekshort-circuits the flow with an earlythrowwhen its condition holds.
Applying it
Signature: export function isoWeek(date = new Date()
The surface worth importing is isoWeek; the helpers around it are private to the snippet.
The call returns year.
Statements that matter
if (!Number.isInteger(year) || year < 1 || year > 9999) throw new RangeError("year out of range");
if (!Number.isInteger(week) || week < 1 || week > 53) throw new RangeError("week out of range");
return new Date(Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate()));
Interactive Tools for This Topic
ISO Week CalculatorFind the ISO 8601 week number, ISO week-year, week start and end dates, and day of year for any calendar date, with strict date validation.ISO 8601 Duration ParserParse ISO 8601 durations like PT1H30M into year, month, week, day, and time components with exact or approximate total seconds.Age CalculatorCalculate exact age in years, months, and days between a birth date and a reference date, plus total days and the next birthday countdown.Business Days CalculatorCount the weekdays between two dates, excluding weekends and any holidays you list, and see how many calendar and weekend days are involved.
FAQ
- Does `isoWeek` modify its input?
- No — a new value is constructed for the result and the arguments are never written to.
- How does the snippet fail?
- Validation comes first — `RangeError` and `TypeError` marks the failure at `check.year !== year || check.week !== week`, before any partial work happens.
- Does it depend on any third-party library?
- No third-party library is referenced; the standard library covers it. The file defines `isoWeek`, `isoWeekStart`, `toUtcDate` at load time.
Report a problem with this exampleOpens a pre-filled issue on GitHub.