Skip to content

fix: parse ISO dates with a year before 1000 - #85

Open
Osamaali313 wants to merge 1 commit into
formkit:mainfrom
Osamaali313:fix-parse-years-before-1000
Open

fix: parse ISO dates with a year before 1000#85
Osamaali313 wants to merge 1 commit into
formkit:mainfrom
Osamaali313:fix-parse-years-before-1000

Conversation

@Osamaali313

Copy link
Copy Markdown

Summary

parse() throws on a valid, zero-padded ISO date whose year is before 1000:

parse("0087-06-15", "YYYY-MM-DD") // throws: Date (0087-06-15) does not match format (YYYY-MM-DD)
parse("0500-01-02", "YYYY-MM-DD") // throws

The cause is the four helper in src/common.ts:

/** Creates a leading zero string of 4 digits. */
export const four = (n: number) => String(n).padStart(2, "0")

It's named four and documented "4 digits", but pads to 2. It's used only in parse.ts to assemble the ISO string passed to new Date():

const isoString = `${four(Y)}-${two(M + 1)}-${two(D)}T...`
const d = new Date(isoString)
if (isFinite(+d)) return d
return invalid()

For Y < 1000, four(Y) yields a 1–3 digit year, so the string (e.g. "87-06-15T...") isn't valid ISO 8601, new Date() is Invalid Date, and parse() throws.

Fix

Pad to 4, as the helper's name and docstring intend:

export const four = (n: number) => String(n).padStart(4, "0")

This is a no-op for years ≥ 1000 (their string is already ≥ 4 chars, so padStart(4) and padStart(2) are identical), so no existing behavior changes. The library already committed to supporting non-4-digit years elsewhere (offset handling, #82); this closes the same gap in parse().

Testing

Added a regression test in parse.spec.ts (parses an ISO date whose year is before 1000). Before the fix it throws; after, it returns the correct date. The full parse suite passes (33/33) and the fix leaves every other test unchanged.

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.
Copilot AI lite review requested due to automatic review settings August 18, 2026 19:06
@vercel

vercel Bot commented Aug 18, 2026

Copy link
Copy Markdown

@Osamaali313 is attempting to deploy a commit to the Formkit Team on Vercel.

A member of the Team first needs to authorize it.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants