Skip to content
Myr

Answer parse-iso-week

Landed as bf199eeanswered parse-iso-week

parseISO reads ISO 8601 week dates (2026-W41-3, the basic 2026W413, and 2026-W41 for the week's Monday) as the UTC midnight of that day, with the same optional time and offset as a calendar date, and gives an invalid date for a week the year doesn't have.

authored by oscar · committed by the platform · parent d88535c · Browse files · 2 files+83 added−7 removed
This landing

State

Landed as bf199eeanswered parse-iso-week

By oscar · the answer and its checks

Credit

Held-back: export 7/7 @ sha256:57d4055d5f282f4ad3c0d7ab56b5adc429956250fbb62f9db2b54172c6042a10; parser 4/4 @ sha256:57d4055d5f282f4ad3c0d7ab56b5adc429956250fbb62f9db2b54172c6042a10

Agents used, disclosed and not credit

  • Assisted-by: claude-code_2-1-300_agent (self-reported)

Diff stats

2 files+83 added−7 removed

  • territory:+44 added−7 removed
  • tests:+39 added−0 removed

2 files+83 added−7 removed

modified:src/parse.ts+44 added-7 removed
modified:test/parse.test.ts+39 added
19 unmodified lines
20 expect(iso("2026280")).toBe("2026-10-07T00:00:00.000Z");21 expect(iso("2028-366")).toBe("2028-12-31T00:00:00.000Z");22 });2324 test("reads a week date as the UTC midnight of its day, and a week as its Monday", () => {25 // Week 41 of 2026 is Monday, October 5 to Sunday, October 11.26 expect(iso("2026-W41-3")).toBe("2026-10-07T00:00:00.000Z");27 expect(iso("2026W413")).toBe("2026-10-07T00:00:00.000Z");28 expect(iso("2026-W41")).toBe("2026-10-05T00:00:00.000Z");29 expect(iso("2026W41")).toBe("2026-10-05T00:00:00.000Z");30 expect(iso("2026-W41-7")).toBe("2026-10-11T00:00:00.000Z");31 });3233 test("puts week 1 around the year's first Thursday, so it can start in the year before", () => {34 expect(iso("2026-W01-1")).toBe("2025-12-29T00:00:00.000Z");35 expect(iso("2021-W01-1")).toBe("2021-01-04T00:00:00.000Z");36 // 2020 starts on a Wednesday and is a leap year, so it has 53 weeks.37 expect(iso("2020-W53-7")).toBe("2021-01-03T00:00:00.000Z");38 });3940 test("gives a week date the same time and offset as a calendar date", () => {41 expect(iso("2026-W41-3T14:30Z")).toBe("2026-10-07T14:30:00.000Z");42 expect(iso("2026-W41-3T09:30:00+02:00")).toBe("2026-10-07T07:30:00.000Z");43 expect(iso("2026-W41T14:30")).toBe("2026-10-05T14:30:00.000Z");44 });4546 test("reads week 53 only in a year with 53 weeks", () => {47 // 2026 starts on a Thursday, so it has 53 weeks; 2027 starts on a Friday and has 52.48 expect(iso("2026-W53-7")).toBe("2027-01-03T00:00:00.000Z");49 expect(isValid(parseISO("2027-W53-1"))).toBe(false);50 expect(isValid(parseISO("2027-W53"))).toBe(false);51 expect(isValid(parseISO("2026-W54-1"))).toBe(false);52 });5354 test("reads a time with no offset as UTC", () => {55 expect(iso("2026-10-07T14:30")).toBe("2026-10-07T14:30:00.000Z");56 expect(iso("2026-10-07T14")).toBe("2026-10-07T14:00:00.000Z");57 expect(iso("2026-10-07T14:30:15.250")).toBe("2026-10-07T14:30:15.250Z");
30 unmodified lines
88 "2026-13-01",89 "2026-10-32",90 "2026-00-10",91 "2026-367",92 "2026-W00-1",93 "2026-W41-0",94 "2026-W41-8",95 "2026-W41-",96 "2026-W41-33",97 "2026-W4-3",98 "2026-w41-3",99 "2026-W413",100 "2026W41-3",101 "2026-10-07T24:00",102 "2026-10-07T14:60",103 "2026-10-07T14:30:00+24:00",104 "2026-10-07T14:30T15:00",
13 unmodified lines