Skip to content

fix(isISO8601): pad year to 4 digits in strict validation for years < 1000#2724

Closed
xxiaoxiong wants to merge 1 commit into
validatorjs:masterfrom
xxiaoxiong:fix/isISO8601-year-less-than-1000-strict-2517
Closed

fix(isISO8601): pad year to 4 digits in strict validation for years < 1000#2724
xxiaoxiong wants to merge 1 commit into
validatorjs:masterfrom
xxiaoxiong:fix/isISO8601-year-less-than-1000-strict-2517

Conversation

@xxiaoxiong
Copy link
Copy Markdown

Description

When isISO8601 is called with strict: true, the isValidDate function extracts the year via Number(), which converts leading-zero years like 0001 to 1. The year is then interpolated directly into the date string passed to new Date().

For example, 1-01-13 is interpreted by JavaScript as January 13th 2001 instead of January 13th 0001, causing the getUTCFullYear() comparison to fail.

Changes

  • Pad the year to 4 digits using padStart(4, '0') before constructing the date string
  • This ensures correct Date parsing for all year values (1-9999)

Related Issue

Fixes #2517

… 1000

When `isISO8601` is called with `strict: true`, the `isValidDate`
function extracts the year via `Number()`, which converts leading-zero
years like `0001` to `1`. The year is then interpolated directly
into the date string passed to `new Date()`, causing ambiguous parsing
(e.g. `1-01-13` is interpreted as January 13th 2001 instead of
January 13th 0001).

This fix pads the year to 4 digits before constructing the date
string, preserving correct parsing for all year values.

Fixes #2517
@codecov
Copy link
Copy Markdown

codecov Bot commented May 28, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (7fdc788) to head (b32435c).

Additional details and impacted files
@@            Coverage Diff            @@
##            master     #2724   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          114       114           
  Lines         2587      2588    +1     
  Branches       656       656           
=========================================
+ Hits          2587      2588    +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@xxiaoxiong xxiaoxiong closed this May 31, 2026
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.

isISO8601 does not support dates with year inferior to 1000 in strict mode

1 participant