Skip to content

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

Description

@ulrik59

Describe the bug
When giving a valid date with year before 1000, the isISO8601 validator is returning false when strict option is set to true.
The issue in the isValidDate function of the src/lib/isISO8601.js file.
image
At line 30, a new Date object is instanced using a string built with year, monthString and dayString.
But contrary to month and day, no leading zeros are added to year. This is causing the object Date to not parse correctly the date and in resulting in the d.getUTCFullYear() === year test to fail.

Examples
Using the valid date 0001-01-13T00:00:00.000Z, the parsed year variable will be equal to 1, month to 1 and day to 13.
The Date object will be constructed using the following string: 1-01-13.
This is interpreted as January 1st 2013 instead of January 13th 0001 which obviously lead to the d.getUTCFullYear() === year test to fail (2013 is different than 1)

Additional context
Validator.js version: v13.12.0
Node.js version: v22.12.0
OS platform: linux

Activity

  1. skpanagiotis commented on Jan 19, 2025

    @skpanagiotis

    I have opened a PR(#2519) for this issue.

  2. uhop commented on Aug 23, 2026

    @uhop

    Still reproducing on 13.15.35, and there is a sharper symptom than the one in
    the original report: because the rebuilt string falls out of the Date Time
    String Format and into the legacy local-time parser, the verdict depends on the
    host timezone.

    const validator = require('validator'); // 13.15.35
    validator.isISO8601('0500-02-28', { strict: true });
    // TZ=UTC             -> true
    // TZ=America/Chicago -> true
    // TZ=Asia/Tokyo      -> false
    
    validator.isISO8601('0000-01-01', { strict: true }); // false in every zone

    So the same input validates on a CI runner in UTC and fails on a developer
    machine in Tokyo. Deriving the comparison arithmetically instead of through
    new Date(...), as PR #2841 does for isRFC3339, would avoid the legacy
    parser entirely and fix the timezone dependence along with the original bug.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions