Emmanuel Akinfulubi

JavaScriptDate

Format an ISO week date (2026-W03-4) in JavaScript

Advanced · 4 min read

An ISO week date writes a day as its year, its week and its weekday: 2026-W03-4 means "2026, week 3, day 4", which is Thursday 15 January 2026. You see this format in factories, logistics, retail planning and some government reports, where everything is organized by week.

In this tutorial you will write isoWeekParts(date), which returns the three numbers, and isoWeekDate(date), which joins them into the text. The tricky part is the year: for a few days around New Year, the year in a week date isn't the calendar year. You need to know Date.UTC, getUTCDay() and padStart. If you have done the ISO week number exercise, most of this will look familiar.

Three parts

  • Week-numbering year: the year that owns the week. It is usually the calendar year, but not always.
  • Week: 1 to 52 or 53, always written with two digits, so week 3 is 03.
  • Weekday: 1 for Monday to 7 for Sunday.

Who owns a week?

In ISO 8601, weeks run Monday to Sunday, and week 1 is the week containing the year's first Thursday. That means every week belongs to the year its Thursday is in. 1 January 2027 is a Friday, and the Thursday of its week is 31 December 2026. So 1 January 2027 is in the 2026 week-numbering year.

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

51 The full ISO week date, 2026-W03-4

hard

Produce the complete ISO week date: week-numbering year, week number and weekday, as 2026-W03-4. Show a date whose week-numbering year is not its calendar year.

Stuck? Three hints, smallest first
  1. 1. The Thursday trick gives you the week-numbering year for free: it is that Thursday’s year.
  2. 2. Weekdays are 1 for Monday through 7 for Sunday, so getDay() || 7 again.
  3. 3. Pad the week number to two digits. 2026-W3-4 is not valid ISO.

Step 1: copy the date into UTC and find its weekday

Step 1

const MS_PER_DAY = 86400000;
const d = new Date(Date.UTC(2026, 0, 15));
const weekday = d.getUTCDay() || 7;
console.log(weekday); // 4

UTC is the world's reference clock, and every UTC day is exactly 24 hours, so the counting below can't be thrown off by daylight saving. getUTCDay() gives 0 for Sunday. || 7 turns that 0 into 7 and leaves 1 to 6 alone, which gives the ISO numbering.

Step 2: jump to the Thursday, and read its year

Step 2

d.setUTCDate(d.getUTCDate() + 4 - weekday);
const weekYear = d.getUTCFullYear();
console.log(weekYear); // 2026

Moving by 4 - weekday days always lands on the Thursday of the same week. That Thursday's year is the week-numbering year. This is the only new idea compared with the week number on its own: the Thursday gives you the year for free.

Step 3: count the weeks

Step 3

const firstOfYear = Date.UTC(weekYear, 0, 1);
const week = Math.ceil(((d.getTime() - firstOfYear) / MS_PER_DAY + 1) / 7);
console.log(week); // 3

This counts the days from 1 January of the week-numbering year to the Thursday, makes it 1-based, divides by 7 and rounds up.

Step 4: wrap it in a function that returns all three

Step 4

function isoWeekParts(date) {
  const d = new Date(Date.UTC(date.getFullYear(), date.getMonth(), date.getDate()));
  const weekday = d.getUTCDay() || 7;
  d.setUTCDate(d.getUTCDate() + 4 - weekday);
  const weekYear = d.getUTCFullYear();
  const firstOfYear = Date.UTC(weekYear, 0, 1);
  const week = Math.ceil(((d.getTime() - firstOfYear) / MS_PER_DAY + 1) / 7);
  return { weekYear, week, weekday };
}
console.log(isoWeekParts(new Date(2026, 0, 15))); // { weekYear: 2026, week: 3, weekday: 4 }

The first line copies the local calendar date into UTC. Notice weekday is read before the jump to Thursday. After the jump it would always be 4.

{ weekYear, week, weekday } is shorthand for { weekYear: weekYear, week: week, weekday: weekday }.

Step 5: the wrong turn, forgetting to pad

Step 5

const unpadded = (date) => {
  const p = isoWeekParts(date);
  return p.weekYear + '-W' + p.week + '-' + p.weekday;
};
console.log(unpadded(new Date(2026, 0, 15))); // '2026-W3-4'

2026-W3-4 isn't valid ISO. The standard requires two digits for the week. Padding also keeps week dates in the right order when they are sorted as text: without it, W10 would sort before W3.

Step 6: the finished format

Step 6

function isoWeekDate(date) {
  const p = isoWeekParts(date);
  return p.weekYear + '-W' + String(p.week).padStart(2, '0') + '-' + p.weekday;
}
console.log(isoWeekDate(new Date(2026, 0, 15)));  // '2026-W03-4'
console.log(isoWeekDate(new Date(2026, 0, 18)));  // '2026-W03-7'
console.log(isoWeekDate(new Date(2025, 11, 29))); // '2026-W01-1'
console.log(isoWeekDate(new Date(2025, 11, 28))); // '2025-W52-7'
console.log(isoWeekDate(new Date(2027, 0, 1)));   // '2026-W53-5'
console.log(isoWeekDate(new Date(2027, 0, 4)));   // '2027-W01-1'

Sunday is day 7, not 0. Monday 29 December 2025 is already in the 2026 week year, while the day before is the last day of 2025's. And 1 January 2027 still belongs to week 53 of 2026. The 2027 week year doesn't start until Monday 4 January.

The full solution

The panel has isoWeekParts and isoWeekDate. It prints a mid-January date, two dates whose week year differs from their calendar year, and the parts object. The checks cover Monday and Sunday of a week, both year boundaries and the padding. Press Run to see them.

The answer

const MS_PER_DAY = 86400000;

function isoWeekParts(date) {
  const d = new Date(Date.UTC(date.getFullYear(), date.getMonth(), date.getDate()));
  const weekday = d.getUTCDay() || 7;                 // 1 Monday ... 7 Sunday

  d.setUTCDate(d.getUTCDate() + 4 - weekday);         // this week's Thursday
  const weekYear = d.getUTCFullYear();                // which owns the week

  const firstOfYear = Date.UTC(weekYear, 0, 1);
  const week = Math.ceil(((d.getTime() - firstOfYear) / MS_PER_DAY + 1) / 7);

  return { weekYear, week, weekday };
}

function isoWeekDate(date) {
  const p = isoWeekParts(date);
  return p.weekYear + '-W' + String(p.week).padStart(2, '0') + '-' + p.weekday;
}

console.log('15/01/2026 ->', isoWeekDate(new Date(2026, 0, 15)));
console.log('29/12/2025 ->', isoWeekDate(new Date(2025, 11, 29)), '<- a 2025 date in the 2026 week year');
console.log('01/01/2027 ->', isoWeekDate(new Date(2027, 0, 1)), '<- a 2027 date in the 2026 week year');
console.log('parts:', isoWeekParts(new Date(2026, 0, 15)));

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('Thursday 15 January 2026', isoWeekDate(new Date(2026, 0, 15)), '2026-W03-4');
test('the Monday of that week', isoWeekDate(new Date(2026, 0, 12)), '2026-W03-1');
test('and its Sunday, which is 7 rather than 0', isoWeekDate(new Date(2026, 0, 18)), '2026-W03-7');
test('29 December 2025 is in the 2026 week year', isoWeekDate(new Date(2025, 11, 29)), '2026-W01-1');
test('28 December 2025 is the last day of the 2025 week year', isoWeekDate(new Date(2025, 11, 28)), '2025-W52-7');
test('1 January 2027 still belongs to the 2026 week year', isoWeekDate(new Date(2027, 0, 1)), '2026-W53-5');
test('the 2027 week year starts on Monday 4 January', isoWeekDate(new Date(2027, 0, 4)), '2027-W01-1');
test('the week number is padded to two digits', isoWeekDate(new Date(2026, 0, 15)).includes('-W03-'), true);
test('and the week year differs from the calendar year for that December date', isoWeekParts(new Date(2025, 11, 29)).weekYear, 2026);

The week-numbering year is a fourth kind of year, alongside the calendar, financial and academic ones, and the sixth assertion is why it matters: 1 January 2027 is 2026-W53-5. Store an ISO week and a calendar year together and one week in every two or three years is filed under the wrong heading.

The padding is not cosmetic — ISO 8601 requires two digits, and unpadded week numbers break exactly the text-sorting property that made ISO dates worth using in the first place.

Common mistakes

  • Using getFullYear() for the year. For a few days around New Year, the week year is different. Use the Thursday's year.
  • Not padding the week. W3 isn't valid ISO, and it sorts after W10 as text.
  • Reading the weekday after the jump. By then it is always Thursday. Save it first.

Try it yourself

  1. Write fromIsoWeekDate('2026-W03-4') that turns the text back into a date. (Hint: 4 January is always in week 1.)
  2. Print the week date of every day from 26 December 2026 to 5 January 2027 and spot where the week year changes.
  3. Write sameIsoWeek(a, b) that checks whether two dates are in the same ISO week, using the year and week parts.

Keep practising

Newsletter

New writing, when it lands.

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