dayjs icon indicating copy to clipboard operation
dayjs copied to clipboard

core bug with locale plugin

Open titanism opened this issue 2 years ago • 7 comments

> dayjs('4/23/22', 'M/DD/YY').locale('en').format('LT')
'12:00 AM'
> dayjs('4/23/22', 'M/DD/YY').locale('en').format('LTS')
'12:00:00 AM'
> dayjs('4/23/22', 'M/DD/YY').locale('en').format('L')
'04/23/2022'
> dayjs('4/23/22', 'M/DD/YY').locale('en').format('LL')
'April 23, 2022'
> dayjs('4/23/22', 'M/DD/YY').locale('en').format('LLL')
'April 23, 2022 12:00 AM'
> dayjs('4/23/22', 'M/DD/YY').locale('en').format('LLLL')
'Saturday, April 23, 2022 12:00 AM'
> dayjs('4/23/22', 'M/DD/YY').locale('en').format('l')
Uncaught TypeError: Cannot read properties of undefined (reading 'replace')
    at /Users/user/Projects/web/node_modules/.pnpm/[email protected]/node_modules/dayjs/plugin/localizedFormat.js:1:676
    at String.replace (<anonymous>)
    at /Users/user/Projects/web/node_modules/.pnpm/[email protected]/node_modules/dayjs/plugin/localizedFormat.js:1:563
    at r.format (/Users/user/Projects/web/node_modules/.pnpm/[email protected]/node_modules/dayjs/plugin/localizedFormat.js:1:761)
    at e.format (/Users/user/Projects/web/node_modules/.pnpm/[email protected]/node_modules/dayjs/plugin/preParsePostFormat.js:1:592)
    at u.format (/Users/user/Projects/web/node_modules/.pnpm/[email protected]/node_modules/dayjs/plugin/utc.js:1:1655)
> dayjs('4/23/22', 'M/DD/YY').locale('zh').format('l')
'2022/4/23'
> dayjs('4/23/22', 'M/DD/YY').locale('de').format('l')
'23.4.2022'

As you can see, there is an uncaught typeerror above for "l", "ll", "lll", and "llll" usage for certain locales, e.g. en.

titanism avatar Apr 11 '23 17:04 titanism

The reason is because the englishFormats object does not contain it.

https://github.com/iamkun/dayjs/blob/00c223b7e92970d07557133994fcb225a6d4c960/src/plugin/localizedFormat/utils.js#L5-L12

Needs to have l, ll, lll, and llll added. Will submit a PR now.

titanism avatar Apr 11 '23 17:04 titanism

This has been tested. Confirmed my PR works per #2282.

Please merge and release to npm with patch version bump.

Ping me back once done please! Thank you @iamkun.

> dayjs('4/23/22', 'M/DD/YY').locale('en').format('l') // this is with the PR
'4/23/2022'

titanism avatar Apr 11 '23 17:04 titanism

I don't agree with your conclusion, because I looked up the source code and found this

const B = b && b.toUpperCase()
return a || formats[b] || englishFormats[b] || t(formats[B])

tendguo avatar Apr 14 '23 06:04 tendguo

@tendguo you are incorrect as that output is undefined, and you get an uncaught exception TypeError. From my tests on the latest version of dayjs it is an error when you require all the locales and then run the above commands.

titanism avatar Apr 14 '23 17:04 titanism

there should be no problem with localizedFormat plugin

https://runkit.com/embed/yzvddg5czfg6

iamkun avatar Apr 16 '23 13:04 iamkun

This still looks to be an issue in 1.11.13: image

tomastan avatar Sep 08 '24 17:09 tomastan

Still seeing this issue as well. I am using the localizedFormat plugin.

ruthst avatar May 29 '25 18:05 ruthst

there should be no problem with localizedFormat plugin

https://runkit.com/embed/yzvddg5czfg6

This is indeed still a real bug.

Weirdly enough it only happen for us when used as a Nuxt plugin, not when used as a singleton (but that's problematic due to cross-request sharing).

TheDutchCoder avatar Aug 23 '25 19:08 TheDutchCoder

Yeah still 100% a bug that we clearly showed how to reproduce but is not resolved and ignored.

titanism avatar Aug 24 '25 00:08 titanism