iamkun / dayjs

⏰ Day.js 2kB immutable date-time library alternative to Moment.js with the same modern API
https://day.js.org
MIT License
47.11k stars 2.31k forks source link

dayjs(value, formats, true).isValid() bug #2608

Open vtplsp opened 8 months ago

vtplsp commented 8 months ago

Describe the bug dayjs(value, formats, true).isValid() always returns false im using is with a post response to convert date to +1 gmt. if(dayjs(value, formats, true).isValid()) then convert code: import * as customParseFormat from 'dayjs/plugin/customParseFormat'; dayjs.extend(customParseFormat)

var formats = [ "DD/MM/YYYY :) HHmmss", "YYYY/MM/DD :) HHmmss.SSS", "YYYY-MM-DDTHH:mm:ssZ", "YYYY-MM-DDTHH:mm:ss.SSSZ", "YYYY-MM-DDTHH:mm:ss.SSS", "DD MM YYYY HH:mm:ss Z", "LLLL", "LLL", "llll", "lll", ];

dayjs('2024-03-25T23:00:00.000Z', formats, true).isValid()

the output will be false and I expect it to return true

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

Information

Shiv-hcr commented 6 months ago

You need to treat your "Z" token as an escape character (documented here). Example:


import dayjs from "dayjs";
import customParseFormat from "dayjs/plugin/customParseFormat.js";
dayjs.extend(customParseFormat);

const date = '2024-03-25T23:00:00.000Z';
const formats = [
    "DD/MM/YYYY :) HHmmss",
    "YYYY/MM/DD :) HHmmss.SSS",
    "YYYY-MM-DDTHH:mm:ssZ",
    "YYYY-MM-DDTHH:mm:ss.SSS[Z]", // Escape character
    "YYYY-MM-DDTHH:mm:ss.SSS",
    "YYYY-MM-DDTHH:mm:ss.SSS",
    "DD MM YYYY HH:mm:ss Z",
    "LLLL",
    "LLL",
    "llll",
    "lll",
];

console.log(dayjs(date, formats, true).isValid()); // returns true