From cd4a67bf78d828ed82a1dd53bbcc77a70a528d04 Mon Sep 17 00:00:00 2001 From: Osamaali313 Date: Tue, 18 Aug 2026 22:06:19 +0300 Subject: [PATCH] fix: parse ISO dates with a year before 1000 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The `four` helper — named "four" and documented "leading zero string of 4 digits" — padded to 2 instead of 4. It is used only in parse() to build the ISO string handed to `new Date()`, so a valid zero-padded year like "0087" produced "87-06-15T…", which is not valid ISO 8601 and made new Date() return Invalid Date, so parse() threw on a perfectly valid input. Pad to 4 as the name/docs intend. This is a no-op for years >= 1000 (padStart is already satisfied), so existing behavior is unchanged. --- src/__tests__/parse.spec.ts | 8 ++++++++ src/common.ts | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/__tests__/parse.spec.ts b/src/__tests__/parse.spec.ts index 48c8e88..50f4c68 100644 --- a/src/__tests__/parse.spec.ts +++ b/src/__tests__/parse.spec.ts @@ -278,4 +278,12 @@ describe("parse", () => { ).toISOString() ).toBe("1989-12-19T07:30:10.000Z") }) + it("parses an ISO date whose year is before 1000", () => { + // Regression: the `four` helper padded to 2 digits, so a valid + // zero-padded year like "0087" produced an invalid ISO string and + // parse() threw instead of returning the date. + const d = parse("0087-06-15", "YYYY-MM-DD") + expect(Number.isNaN(+d)).toBe(false) + expect(d.getUTCFullYear()).toBe(87) + }) }) diff --git a/src/common.ts b/src/common.ts index 92d911d..7982969 100644 --- a/src/common.ts +++ b/src/common.ts @@ -159,7 +159,7 @@ export const two = (n: number) => String(n).padStart(2, "0") * Creates a leading zero string of 4 digits. * @param n - A number. */ -export const four = (n: number) => String(n).padStart(2, "0") +export const four = (n: number) => String(n).padStart(4, "0") /** * Normalizes a given part to NFKC.