Skip to content

fix(localizedFormat): fall back to English formats for lowercase tokens - #3217

Open
shoutoutuoadi325 wants to merge 1 commit into
iamkun:devfrom
shoutoutuoadi325:fix/localizedformat-lowercase-fallback
Open

shoutoutuoadi325 wants to merge 1 commit into
iamkun:devfrom
shoutoutuoadi325:fix/localizedformat-lowercase-fallback

Conversation

@shoutoutuoadi325

Copy link
Copy Markdown

Summary

format('l') (and ll, lll, llll) throws TypeError: Cannot read properties of undefined (reading 'replace') when the current locale has no formats object, for example a custom locale registered via dayjs.locale(name, { name, weekdays, months }) (allowed by Partial<ILocale>). Parsing has the same hole: dayjs('2/5/2018', 'l', locale) swallows the error and returns Invalid Date.

Uppercase L / LL / ... already falls back to englishFormats, so the lowercase variants should too. The last fallback becomes t(formats[B] || englishFormats[B]), and formats gets a {} default since customParseFormat passes locale.formats, which can be undefined.

Locales that do define formats keep deriving lowercase tokens from their own uppercase formats. Adding lowercase keys to englishFormats directly would shadow those with the US formats (es l would become 3/5/2024 instead of 5/3/2024).

This covers the cases still reproducing in #2281: the en repro there was fixed by the extend-time patch, but any locale without a formats object still crashes.

Test plan

  • locale without formats: l / ll / lll / llll format the same as moment (new test in localizedFormat.test.js)
  • parsing L, l and l LT in such a locale returns the parsed date instead of Invalid Date (new test in customParseFormat.test.js)
  • npm test (all TZ variants, 100% line coverage threshold) and npm run lint pass

Copilot AI lite review requested due to automatic review settings September 17, 2026 06:47

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants