Custom parse format problem
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
- Day.js Version: 1.11.9
- OS: MacOs
- Browser: Edge 113
- Time zone: UTC
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
Sorry, I can't help but I think it's worth mentioning that according to the docs, "." is a recognized separator.
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.
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 🙂
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.
The matchWord pattern in src/plugin/customParseFormat/index.js is missing the ., so MMM. doesn't work. "." as a separator should work anywhere else. So, for example:
dayjs('Mar. 19, 2025 12:00 am', 'MMM. D, YYYY h:mm a').isValid()
// => false
dayjs('Mar . 19, 2025 12:00 am', 'MMM . D, YYYY h:mm a').isValid()
// => true
dayjs('.Mar .19.2025.12:00.am', '.MMM .D.YYYY.h:mm.a').isValid()
// => true
The fix should be as simple as updating matchWord to match the pattern above it in formattingTokens:
const matchWord = /\d*[^-_:/.,()\s\d]+/ // Word