iamkun / dayjs

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

Custom parse format problem #2412

Open shadoworion opened 1 year ago

shadoworion commented 1 year ago

Describe the bug For some reason, my date is not valid but format is correct.

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

dayjs("Sep. 27, 2017, 11:23 AM", "MMM. DD, YYYY, HH:mm A").isValid(); // => false
dayjs("Sep. 27, 2017, 11:23 AM", "MMM. DD, YYYY, hh:mm A").isValid(); // => false

https://jsfiddle.net/asLpb0cz/

Expected behavior To be valid

Information

shadoworion commented 1 year ago

Ok, the problem is "." but why?

If I replace "." with "," it works. dayjs("Sep, 27, 2017, 11:23 AM", "MMM, DD, YYYY, hh:mm A").isValid(); // => true

maciejp commented 1 year ago

Sorry, I can't help but I think it's worth mentioning that according to the docs, "." is a recognized separator.

shadoworion commented 1 year ago

Sorry, I can't help but I think it's worth mentioning that according to the docs, "." is a recognized separator.

":", "/", "," - are also recognized separators. Appear to your logic they also may not work.

maciejp commented 1 year ago

Sorry, I should've phrased my message differently.

I just wanted to highlight that the behavior you expect matches the documentation (as you used a recognized separator), so it seems like it really is a bug 🙂

bolekro commented 2 months ago

Same issue here, e.g. cannot use the lib having such format:

dayjs("2022-06-08-14.42.19", "YYYY-MM-DD-HH.mm.ss").isValid() // false, expected true

After years of using Dayjs, regrettably had to switch to momentjs (package size penalty) which handles this and similar cases correctly.