Skip to content

fix: don't crash parseDate when dateFormat contains timezone tokens#6310

Open
xianjianlf2 wants to merge 1 commit into
Hacker0x01:mainfrom
xianjianlf2:fix/parse-timezone-token-6303
Open

fix: don't crash parseDate when dateFormat contains timezone tokens#6310
xianjianlf2 wants to merge 1 commit into
Hacker0x01:mainfrom
xianjianlf2:fix/parse-timezone-token-6303

Conversation

@xianjianlf2

Copy link
Copy Markdown

Description

Linked issue: #6303

Problem
When dateFormat contains a timezone token (e.g. "MMMM d, yyyy h:mm aa (zzz)"), typing anything into the datepicker input crashes the page. date-fns parse() throws a RangeError ("Format string contains an unescaped latin alphabet character z") because z/zzz are format-only tokens that parse cannot consume. The error propagates out of parseDate into handleChange, so the component throws and the user's typed input is lost.

Changes

  • src/date_utils.ts: wrapped the date-fns parse() call inside parseDate's format loop in a try/catch. When parse throws on an unparseable token, the loop continues, so parsing falls through to the existing native Date fallback (when strictParsing is false) instead of crashing. Behavior for all parseable formats is unchanged.
  • src/test/date_utils_test.test.ts: added a regression test that parses "July 6, 2026 3:30 PM (GMT+0)" against "MMMM d, yyyy h:mm aa (zzz)", asserting parseDate does not throw and the date/time still resolves via the native Date fallback.

Screenshots

N/A (no visual changes)

To reviewers

zzz renders fine through formatDate (date-fns format supports it), so users legitimately configure it in dateFormat; only the parsing path threw. The try/catch also covers any other format-only token parse rejects.

Test run: yarn jest src/test/date_utils_test.test.ts — 236 passed. Prettier and ESLint pass on the touched files.

Fixes #6303

Contribution checklist

  • I have followed the contributing guidelines.
  • I have added sufficient test coverage for my changes.
  • I have formatted my code with Prettier and checked for linting issues with ESLint for code readability.

…oken

date-fns `parse` throws a RangeError for format-only tokens such as
`z`/`zzz`, which crashed handleChange and discarded the user's typed
input whenever `dateFormat` included a timezone (e.g.
"MMMM d, yyyy h:mm aa (zzz)"). Guard the parse call so the loop can fall
through to the native Date fallback and preserve the typed date/time.

Closes Hacker0x01#6303
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.

Specifying (zzz) in the date format prevents typing an input

1 participant