dayjs icon indicating copy to clipboard operation
dayjs copied to clipboard

dayjs("25-AUG-2023", "DD-MMM-YYYY") give invalid date error

Open weihe8892 opened this issue 1 year ago • 2 comments

I am in the process of migrating from moment.js to day.js. While the format dayjs("25-AUG-2023", "DD-MMM-YYYY") results in an "invalid date" error with day.js, it works perfectly with moment.js. I've found that changing it to dayjs("25-Aug-2023", "DD-MMM-YYYY") works, but we have numerous dates with uppercase months. Is there a way to make the "MMM" format less restrictive in day.js?

weihe8892 avatar Aug 25 '23 17:08 weihe8892

Hi, @weihe8892. You can use updateLocale plugin (https://day.js.org/docs/en/customization/customization) to achieve the expected behavior:

dayjs.updateLocale('en', {
  monthsShort: ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC'],
})
dayjs('25-AUG-2023', 'DD-MMM-YYYY') // will work as expected

But in this case you will modify it globally, so:

  • dayjs('25-Aug-2023', 'DD-MMM-YYYY') will not work
  • dayjs().format('MMM') will return AUG, not Aug as by default

Pareder avatar Aug 31 '23 08:08 Pareder

We also ran into this and for now we're able to get around it by just dropping the format and using dayjs('25-AUG-2023') which seems to parse it fine even with any casing for the month. Not sure there's even really a need to pass the format anymore, it's just what we had before with moment.

johnhwright avatar Apr 11 '24 22:04 johnhwright