Emmanuel Akinfulubi

JavaScriptBasic

Get the ISO week number of a date in JavaScript

Advanced · 4 min read

Many offices plan in weeks: "the release is in week 14", "timesheets for week 25". Those numbers usually follow a standard called ISO 8601, and JavaScript has no built-in method for it. You have to work it out yourself.

In this tutorial you will write isoWeek(date), which returns the ISO week number of any date. The rule has a few surprises, like 1 January belonging to week 53 of the year before, so we'll take it slowly. You need to know how to create a date, and that months count from 0 (January is 0). Everything else is explained below.

The ISO week rules

  1. Weeks start on Monday and end on Sunday.
  2. Week 1 is the week that contains the first Thursday of January. That's the same as saying the week that contains 4 January.
  3. So every week has exactly one Thursday, and the Thursday decides which year the week belongs to.

Because of rule 2, the first few days of January can belong to the last week of the year before, and the last few days of December can belong to week 1 of the next year.

For example, 1 January 2026 is a Thursday, so the week from Monday 29 December 2025 to Sunday 4 January 2026 is week 1 of 2026.

Monday-first day numbers

getDay() gives 0 for Sunday, 1 for Monday, up to 6 for Saturday. ISO wants Monday first. (getDay() + 6) % 7 turns Monday into 0 and Sunday into 6:

JavaScript

const isoDay = (d) => (d.getDay() + 6) % 7;
console.log(isoDay(new Date(2026, 0, 5))); // 0: Monday 5 January
console.log(isoDay(new Date(2026, 0, 4))); // 6: Sunday 4 January
console.log(isoDay(new Date(2026, 0, 1))); // 3: Thursday 1 January

Adding 6 and taking the remainder after 7 moves every day back by one place, and Sunday wraps round to the end.

Here is the exercise. Try it on your own first if you like, or read on and we will build it together.

128 Which ISO week a date falls in

hard

Give the ISO-8601 week number of a date. Week 1 is the week containing the first Thursday of January, weeks start on Monday, and a date in early January can belong to week 52 or 53 of the year before.

Stuck? Three hints, smallest first
  1. 1. getDay() counts from Sunday, so (getDay() + 6) % 7 renumbers it Monday-first.
  2. 2. Every ISO week is identified by its Thursday. Move the date to the Thursday of its own week first.
  3. 3. Then count whole weeks from the Thursday of week 1, which is the week containing 4 January of the Thursday’s year.

Step 1: move to the Thursday of the same week

Since the Thursday decides everything, the first move is to find the Thursday of the date's own week. Thursday is ISO day 3, so subtract the date's ISO day and add 3:

Step 1

const isoDay = (d) => (d.getDay() + 6) % 7;
function thursdayOf(date) {
  const thursday = new Date(date.getFullYear(), date.getMonth(), date.getDate());
  thursday.setDate(thursday.getDate() - isoDay(thursday) + 3);
  return thursday;
}
console.log(thursdayOf(new Date(2026, 0, 5)).getDate()); // 8: Monday 5 to Thursday 8
console.log(thursdayOf(new Date(2026, 0, 4)).getDate()); // 1: Sunday 4 back to Thursday 1

We build a copy of the date first. setDate changes the date it's called on, and we must not move the caller's date. Building the copy from year, month and day also drops the time of day.

setDate with a number below 1 or above the month's length rolls into the neighboring month, so this works across month and year boundaries.

Step 2: find the Thursday of week 1

Week 1 is the week containing 4 January. Its Thursday is found the same way:

Step 2

const isoDay = (d) => (d.getDay() + 6) % 7;
const firstThursday = new Date(2026, 0, 4);
firstThursday.setDate(firstThursday.getDate() - isoDay(firstThursday) + 3);
console.log(firstThursday.getMonth(), firstThursday.getDate()); // 0 1: Thursday 1 January

Step 3: count the weeks between them

Subtracting two dates gives the gap in milliseconds. Divide by the milliseconds in a week, and add 1 because the first Thursday is week 1, not week 0:

Step 3

const WEEK_MS = 7 * 24 * 60 * 60 * 1000;
const isoDay = (d) => (d.getDay() + 6) % 7;
function isoWeek(date) {
  const thursday = new Date(date.getFullYear(), date.getMonth(), date.getDate());
  thursday.setDate(thursday.getDate() - isoDay(thursday) + 3);
  const firstThursday = new Date(date.getFullYear(), 0, 4);
  firstThursday.setDate(firstThursday.getDate() - isoDay(firstThursday) + 3);
  return 1 + Math.round((thursday - firstThursday) / WEEK_MS);
}
console.log(isoWeek(new Date(2026, 0, 5)));   // 2
console.log(isoWeek(new Date(2026, 5, 15)));  // 25
console.log(isoWeek(new Date(2025, 11, 29))); // 53, but it should be 1
console.log(isoWeek(new Date(2027, 0, 1)));   // 0, which is not a week at all

The middle of the year is fine. The edges are wrong. 29 December 2025 is in week 1 of 2026, and 1 January 2027 can't be week 0.

Math.round is there because, in places with daylight saving time, two local dates some weeks apart may not be an exact number of 24-hour days apart. Rounding absorbs the hour.

Step 4: use the Thursday's year

The bug is date.getFullYear() on the line that builds firstThursday. The week belongs to the year of its Thursday, not the year of the date we were given. Change one word:

Step 4

const WEEK_MS = 7 * 24 * 60 * 60 * 1000;
const isoDay = (d) => (d.getDay() + 6) % 7;
function isoWeek(date) {
  const thursday = new Date(date.getFullYear(), date.getMonth(), date.getDate());
  thursday.setDate(thursday.getDate() - isoDay(thursday) + 3);
  const firstThursday = new Date(thursday.getFullYear(), 0, 4);
  firstThursday.setDate(firstThursday.getDate() - isoDay(firstThursday) + 3);
  return 1 + Math.round((thursday - firstThursday) / WEEK_MS);
}
console.log(isoWeek(new Date(2025, 11, 29))); // 1
console.log(isoWeek(new Date(2026, 11, 31))); // 53
console.log(isoWeek(new Date(2027, 0, 1)));   // 53

Now 29 December 2025 moves to Thursday 1 January 2026 and counts from the start of 2026. And 1 January 2027 moves to Thursday 31 December 2026, so it's in week 53 of 2026.

That last line is why a report grouped by week number needs the ISO year too. "Week 53" on 1 January 2027 means a week of 2026.

The full solution

The answer panel has isoWeek with the Thursday's year, and prints the week for six dates around the year edges. The checks confirm that 2026 has 53 weeks, the input date is not moved, and all seven days of one week report the same number. Press Run to see the output.

The answer

const WEEK_MS = 7 * 24 * 60 * 60 * 1000;
const stamp = (d) =>
  d.getFullYear() + '-' + String(d.getMonth() + 1).padStart(2, '0') + '-' + String(d.getDate()).padStart(2, '0');

// Monday is 0 and Sunday is 6, which is what ISO counts.
const isoDay = (d) => (d.getDay() + 6) % 7;

function isoWeek(date) {
  // Copy, and drop the time, so the original is untouched and DST cannot creep in.
  const thursday = new Date(date.getFullYear(), date.getMonth(), date.getDate());
  thursday.setDate(thursday.getDate() - isoDay(thursday) + 3);

  // Week 1 is the week holding 4 January OF THE THURSDAY'S YEAR, not of the
  // date's year. That one word is what makes 1 January 2027 come out as week 53.
  const firstThursday = new Date(thursday.getFullYear(), 0, 4);
  firstThursday.setDate(firstThursday.getDate() - isoDay(firstThursday) + 3);

  return 1 + Math.round((thursday - firstThursday) / WEEK_MS);
}

const dates = [new Date(2025, 11, 29), new Date(2026, 0, 1), new Date(2026, 0, 5), new Date(2026, 5, 15), new Date(2026, 11, 31), new Date(2027, 0, 1)];
for (const d of dates) console.log(d.getDate() + '/' + (d.getMonth() + 1) + '/' + d.getFullYear(), '-> week', isoWeek(d));

Output


      
9 checks that prove this answer

You do not have to read these. They are part of the answer panel above, so pressing Run there executes all 9 of them and prints ok for each. That output is the proof, and the build fails if a single check fails.

9 checks

test('1 January 2026 is a Thursday, so it is in week 1', [new Date(2026, 0, 1).getDay(), isoWeek(new Date(2026, 0, 1))], [4, 1]);
test('week 1 of 2026 starts on 29 December 2025', isoWeek(new Date(2025, 11, 29)), 1);
test('Sunday 4 January is the last day of that week', isoWeek(new Date(2026, 0, 4)), 1);
test('and the Monday after it opens week 2', isoWeek(new Date(2026, 0, 5)), 2);
test('mid-June is week 25', isoWeek(new Date(2026, 5, 15)), 25);
test('2026 starts on a Thursday, so it is a 53-week year', isoWeek(new Date(2026, 11, 31)), 53);
test('which is why 1 January 2027 is week 53, not week 1', isoWeek(new Date(2027, 0, 1)), 53);
test('the date passed in is not moved, only the copy is', (() => { const d = new Date(2026, 5, 15); isoWeek(d); return stamp(d); })(), '2026-06-15');
test('and every day of one ISO week reports the same number', [...new Set(Array.from({ length: 7 }, (_, i) => isoWeek(new Date(2026, 5, 15 + i))))], [25]);

ISO weeks are defined by Thursdays, and once you know that the algorithm is short: shift the date to the Thursday of its own week, shift 4 January to the Thursday of week 1, and divide the gap by seven days. Everything else is bookkeeping. (getDay() + 6) % 7 is the renumbering from JavaScript's Sunday-first days to ISO's Monday-first, and forgetting it puts every Sunday in the wrong week.

The subtle line is new Date(thursday.getFullYear(), 0, 4). It uses the year of the Thursday, not the year of the input date, and that is deliberate: 1 January 2027 sits in the week whose Thursday is 31 December 2026, so it belongs to week 53 of 2026. Calendar year and ISO year are different things, and any report grouped by "week 1" without also carrying the ISO year will merge two different weeks.

Math.round rather than a plain division is protection against clocks that shift. Local dates a few weeks apart are not always an exact multiple of 24 hours when daylight saving intervenes, so the quotient comes out at 24.958 and truncation loses a week. Nigeria has no DST, but the reader's machine might.

Common mistakes

  • Using getDay() as it is. It counts from Sunday, so every Sunday lands in the wrong week. Convert with (getDay() + 6) % 7.
  • Using the date's year for week 1. Dates at the very start or end of a year then get week 0 or the wrong week 53.
  • Calling setDate on the date you were given. It mutates the caller's date. Work on a copy.

Try it yourself

  1. Write isoYear(date) that returns the year of the date's Thursday. Check that 1 January 2027 gives 2026.
  2. Combine the two into a label like 2026-W03, with the week padded to two digits.
  3. Find the first year after 2026 that has 53 ISO weeks by checking 28 December of each year.

Keep practising

Newsletter

New writing, when it lands.

Occasional notes on shipping software with AI agents. No schedule, no filler, unsubscribe in one click.