WhatWeek

JavaScript Week Number

Safe ISO week helper

function getIsoWeek(dateString) {
  const date = new Date(dateString + "T00:00:00.000Z");
  const weekday = date.getUTCDay() || 7;
  date.setUTCDate(date.getUTCDate() + 4 - weekday);
  const weekYear = date.getUTCFullYear();
  const weekOne = new Date(Date.UTC(weekYear, 0, 4));
  const weekOneDay = weekOne.getUTCDay() || 7;
  weekOne.setUTCDate(weekOne.getUTCDate() + 1 - weekOneDay);
  const weekNumber = Math.floor((date - weekOne) / 86400000 / 7) + 1;
  return { weekYear, weekNumber };
}
const result = getIsoWeek("2026-05-03");
console.log(result);
// { weekYear: 2026, weekNumber: 18 }

Safe date parsing

Use explicit YYYY-MM-DD strings and append a UTC time when the calculation should be timezone-neutral.

Avoid passing a bare date string into business logic if the result depends on a user's local timezone. Decide whether the input means a UTC date or a local calendar date before calculating the week.

Timezone warning

For current-week features, calculate the user's local calendar date first, then calculate the week number from that date.

Edge-case test table

Input Expected ISO week What it catches
2025-12-29 2026-W01 Week-year rollover before January.
2026-12-28 2026-W53 Valid ISO Week 53 handling.
2027-01-04 2027-W01 Transition out of Week 53.

API option

Use the WhatWeek API when you need stable JSON responses for applications or internal tools.