Emmanuel Akinfulubi

JavaScriptDate

Calculate the ISO 8601 week number in JavaScript

Advanced · 4 min read

"Week 3" is how many offices, factories and schools plan their year. Sprint boards, shift rotas and delivery schedules are often organized by week number. Most of them use the international standard, ISO 8601, which has very exact rules about where week 1 starts.

In this tutorial you will write isoWeek(date), which returns the ISO week number of a date. You will see why the obvious "day of the year divided by 7" gives the wrong answer, and how one clever step, jumping to the Thursday of the week, solves the whole problem. You need to know getDay(), Date.UTC and how to make a date from parts.

The ISO rules

ISO 8601 numbers weeks like this:

  1. Weeks run from Monday to Sunday.
  2. Week 1 is the week that contains the year's first Thursday.
  3. So a year has 52 or 53 weeks, and the first or last few days of January or December can belong to a week of the neighboring 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, even though three of its days are in 2025.

The Thursday trick

Rule 2 has a neat consequence: every week belongs to the year that its Thursday is in. So if you move any date to the Thursday of its own week, that Thursday tells you the year, and counting Thursdays from 1 January tells you the week number. No special cases are needed.

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

50 The ISO week number

hard

Compute the ISO 8601 week number. Week 1 is the week containing the first Thursday of the year, weeks run Monday to Sunday, and a year can have 53 of them.

Stuck? Three hints, smallest first
  1. 1. Move the date to the Thursday of its own week first. Whatever year that Thursday is in owns the week.
  2. 2. getDay() || 7 converts Sunday from 0 to 7, giving the ISO numbering where Monday is 1.
  3. 3. Work in UTC throughout, so a clock change cannot make a day 23 hours long and skew the division.

Step 1: the wrong turn, day of year divided by 7

The first idea is to count the days since 1 January and divide by 7:

Step 1

const MS_PER_DAY = 86400000;
const naiveWeek = (date) => {
  const dayOfYear = (Date.UTC(date.getFullYear(), date.getMonth(), date.getDate())
    - Date.UTC(date.getFullYear(), 0, 1)) / MS_PER_DAY + 1;
  return Math.ceil(dayOfYear / 7);
};
console.log(naiveWeek(new Date(2026, 0, 5)));   // 1
console.log(naiveWeek(new Date(2025, 11, 29))); // 52

Monday 5 January 2026 starts ISO week 2, and 29 December 2025 is in ISO week 1 of 2026. The naive version gets both wrong, because it ignores where Monday falls and which Thursday owns the week.

Step 2: work in UTC

Copy the calendar date into UTC first:

Step 2

const asUtc = (date) => new Date(Date.UTC(date.getFullYear(), date.getMonth(), date.getDate()));
console.log(asUtc(new Date(2026, 0, 15)).getUTCDate()); // 15

UTC is the world's reference clock, and every UTC day is exactly 24 hours. Local days can be 23 or 25 hours long when clocks change for daylight saving time. If one of those days were in the range, dividing by 86,400,000 could land on the wrong side of a whole number and shift the week by one.

Step 3: number the days Monday 1 to Sunday 7

JavaScript counts Sunday as 0. ISO counts Monday as 1 and Sunday as 7. || fixes the one value that differs:

Step 3

console.log(asUtc(new Date(2026, 0, 15)).getUTCDay() || 7); // 4
console.log(asUtc(new Date(2026, 0, 18)).getUTCDay() || 7); // 7

a || b gives b only when a is a "falsy" value such as 0. So Sunday's 0 becomes 7 and every other day stays the same.

Step 4: jump to this week's Thursday

Thursday is day 4. To get there from day n, move 4 - n days. On a Monday that is +3, on a Sunday it is -3:

Step 4

const d = asUtc(new Date(2025, 11, 29)); // Monday 29 December 2025
const dayNumber = d.getUTCDay() || 7;
d.setUTCDate(d.getUTCDate() + 4 - dayNumber);
console.log(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate()); // 2026 0 1

The Thursday of that week is 1 January 2026. So this Monday belongs to the 2026 week-numbering year. setUTCDate rolls over the year end for us.

Step 5: count from 1 January of that year

Now count the days from 1 January of the Thursday's year, add 1 so the first day is day 1, divide by 7 and round up:

Step 5

function isoWeek(date) {
  const d = asUtc(date);
  const dayNumber = d.getUTCDay() || 7;
  d.setUTCDate(d.getUTCDate() + 4 - dayNumber);
  const firstOfYear = Date.UTC(d.getUTCFullYear(), 0, 1);
  return Math.ceil(((d.getTime() - firstOfYear) / MS_PER_DAY + 1) / 7);
}
console.log(isoWeek(new Date(2026, 0, 15))); // 3

For 15 January 2026, a Thursday, that is 14 days after 1 January, so day 15. 15 divided by 7 is about 2.14, and rounding up gives week 3.

Step 6: the edges of the year

Step 6

console.log(isoWeek(new Date(2025, 11, 28))); // 52
console.log(isoWeek(new Date(2025, 11, 29))); // 1
console.log(isoWeek(new Date(2026, 0, 4)));   // 1
console.log(isoWeek(new Date(2026, 0, 5)));   // 2
console.log(isoWeek(new Date(2026, 11, 31))); // 53

Sunday 28 December 2025 is the last day of week 52 of 2025. The next day starts week 1 of 2026. Sunday 4 January is still week 1, and Monday 5 January starts week 2. And 31 December 2026 is in week 53, which only some years have.

Because a week number can belong to the next or previous year, never pair it with the date's own calendar year. The next exercise builds the full week date, with its own week-numbering year.

The full solution

The panel has isoWeek. It prints both sides of the 2025/2026 boundary, a mid-January date and the 53rd week. The checks cover the first Thursday, both ends of a week, the year boundary and a whole week of days with the same number. Press Run to see them.

The answer

const MS_PER_DAY = 86400000;

function isoWeek(date) {
  // Re-express the local calendar date as UTC, where every day is 24 hours.
  const d = new Date(Date.UTC(date.getFullYear(), date.getMonth(), date.getDate()));

  const dayNumber = d.getUTCDay() || 7;              // Monday 1 ... Sunday 7
  d.setUTCDate(d.getUTCDate() + 4 - dayNumber);      // jump to this week's Thursday

  // That Thursday's year is the week-numbering year, by definition.
  const firstOfYear = Date.UTC(d.getUTCFullYear(), 0, 1);
  return Math.ceil(((d.getTime() - firstOfYear) / MS_PER_DAY + 1) / 7);
}

// 1 January 2026 is a Thursday, so week 1 of 2026 starts on Monday 29 December 2025.
console.log('29/12/2025 ->', isoWeek(new Date(2025, 11, 29)), '<- already week 1 of 2026');
console.log('28/12/2025 ->', isoWeek(new Date(2025, 11, 28)), '<- still week 52 of 2025');
console.log('15/01/2026 ->', isoWeek(new Date(2026, 0, 15)));
console.log('31/12/2026 ->', isoWeek(new Date(2026, 11, 31)), '<- 2026 has 53 weeks');

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('15 January 2026 is in week 3', isoWeek(new Date(2026, 0, 15)), 3);
test('1 January 2026 is in week 1, because it is a Thursday', isoWeek(new Date(2026, 0, 1)), 1);
test('the Sunday of that week is still week 1', isoWeek(new Date(2026, 0, 4)), 1);
test('the Monday after starts week 2', isoWeek(new Date(2026, 0, 5)), 2);
test('29 December 2025 belongs to week 1 of 2026', isoWeek(new Date(2025, 11, 29)), 1);
test('while 28 December 2025 is week 52 of 2025', isoWeek(new Date(2025, 11, 28)), 52);
test('31 December 2026 is in week 53, which not every year has', isoWeek(new Date(2026, 11, 31)), 53);
test('every day from 12 to 18 January 2026 is in the same week', Array.from({ length: 7 }, (unused, i) => isoWeek(new Date(2026, 0, 12 + i))), [3, 3, 3, 3, 3, 3, 3]);
test('and the day before that block is in week 2', isoWeek(new Date(2026, 0, 11)), 2);

Everything about this algorithm follows from one definition: the week belongs to whichever year contains its Thursday. Move to that Thursday first and the rest is arithmetic — no special cases for the start or end of the year, and the 53-week years fall out on their own.

Two details are easy to miss. getDay() || 7 is needed because ISO counts Monday as 1 and Sunday as 7, while JavaScript counts Sunday as 0. And the whole calculation is done in UTC: exercise 11 showed that a local day can be 23 or 25 hours long, which would make the division by 86,400,000 land on the wrong side of an integer boundary and shift a week number by one for anyone in a daylight-saving zone.

The most quoted consequence is in the fifth and sixth assertions: 29 December 2025 is in week 1 of 2026, and the last days of December can carry the following year's week number. Never pair an ISO week number with the calendar year — pair it with the week-numbering year, which is the next exercise.

Common mistakes

  • Dividing the day of the year by 7. It ignores where Monday falls, so many dates come out one week off.
  • Treating Sunday as day 0. ISO weeks end on Sunday, day 7. Use getUTCDay() || 7.
  • Counting in local time. A 23- or 25-hour day can shift the result. Do the whole calculation in UTC.

Try it yourself

  1. Write weeksInYear(year): the ISO week number of 28 December is always the last week of that year.
  2. Print the ISO week number of the first day of every month in 2026.
  3. Write mondayOfIsoWeek(date) that returns the Monday that starts the date's ISO week.

Keep practising

Newsletter

New writing, when it lands.

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