Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: validation of years with arbitrary number of digits with YYYY is not working #2661

Open
maxkomarychev opened this issue May 15, 2024 · 1 comment

Comments

@maxkomarychev
Copy link

Describe the bug

Validation does not work properly with the strict flag https://day.js.org/docs/en/parse/is-valid

All these dates are considered valid even though the "YYYY" is used and there are dates which have <4 digits in year:

> const dayjs = require('dayjs')
undefined
> dayjs('2022-02-31', 'YYYY-MM-DD', true).isValid();
true
> 
> dayjs('2022-02-31', 'YYYY-MM-DD', true).isValid();
true
> dayjs('202-02-31', 'YYYY-MM-DD', true).isValid();
true
> dayjs('02-02-31', 'YYYY-MM-DD', true).isValid();
true
> dayjs('2-02-31', 'YYYY-MM-DD', true).isValid();
true

Expected behavior
A clear and concise description of what you expected to happen.

"YYYY" must only allow 4-digit years per spec

https://day.js.org/docs/en/parse/string-format

image

Information

  • Day.js Version: 1.11.11
  • OS: macos
  • Browser: N/A node
  • Time zone: CET
@maxkomarychev maxkomarychev changed the title bug: validation of 4 digit years with YYYY is not working bug: validation of years with arbitrary number of digits with YYYY is not working May 15, 2024
@Shiv-hcr
Copy link

Shiv-hcr commented May 30, 2024

Unable to replicate this issue in Node.js. The custom format requires the "customParseFormat" plugin - are you testing with this plugin imported too? I've written a quick test in Node:

import dayjs from "dayjs";

const format = "YYYY-MM-DD";
const strictMode = true;
const dates = [
    "2024-05-30", // control - should be true for both tests
    "2022-02-31",
    "202-02-31",
    "02-02-31",
    "2-02-31",
];

console.log("Test without plugin:")
for (const date of dates) {
    console.log(`Date: ${date}. Result: ${dayjs(date, format, strictMode).isValid()}`);
}

console.log("\nTest with plugin:")
import customParseFormat from "dayjs/plugin/customParseFormat.js";
dayjs.extend(customParseFormat);

for (const date of dates) {
    console.log(`Date: ${date}. Result: ${dayjs(date, format, strictMode).isValid()}`);
}

Output:

Test without plugin:
Date: 2024-05-30. Result: true
Date: 2022-02-31. Result: true
Date: 202-02-31. Result: true
Date: 02-02-31. Result: true
Date: 2-02-31. Result: true

Test with plugin:
Date: 2024-05-30. Result: true
Date: 2022-02-31. Result: false
Date: 202-02-31. Result: false
Date: 02-02-31. Result: false
Date: 2-02-31. Result: false

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

No branches or pull requests

2 participants