dayjs icon indicating copy to clipboard operation
dayjs copied to clipboard

Custom parse format problem

Open shadoworion opened this issue 2 years ago • 6 comments

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

shadoworion avatar Aug 14 '23 17:08 shadoworion

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

shadoworion avatar Aug 14 '23 18:08 shadoworion

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

maciejp avatar Oct 17 '23 08:10 maciejp

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.

shadoworion avatar Oct 17 '23 09:10 shadoworion

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 🙂

maciejp avatar Oct 17 '23 09:10 maciejp

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.

bolekro avatar Aug 25 '24 14:08 bolekro

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

Xenoveritas avatar Mar 19 '25 05:03 Xenoveritas