dayjs
dayjs copied to clipboard
dayjs("25-AUG-2023", "DD-MMM-YYYY") give invalid date error
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?
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 returnAUG
, notAug
as by default
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.