fix: don't crash parseDate when dateFormat contains timezone tokens#6310
Open
xianjianlf2 wants to merge 1 commit into
Open
fix: don't crash parseDate when dateFormat contains timezone tokens#6310xianjianlf2 wants to merge 1 commit into
xianjianlf2 wants to merge 1 commit into
Conversation
…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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Linked issue: #6303
Problem
When
dateFormatcontains a timezone token (e.g."MMMM d, yyyy h:mm aa (zzz)"), typing anything into the datepicker input crashes the page. date-fnsparse()throws aRangeError("Format string contains an unescaped latin alphabet characterz") becausez/zzzare format-only tokens thatparsecannot consume. The error propagates out ofparseDateintohandleChange, so the component throws and the user's typed input is lost.Changes
src/date_utils.ts: wrapped the date-fnsparse()call insideparseDate's format loop in atry/catch. Whenparsethrows on an unparseable token, the loopcontinues, so parsing falls through to the existing nativeDatefallback (whenstrictParsingis 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)", assertingparseDatedoes not throw and the date/time still resolves via the nativeDatefallback.Screenshots
N/A (no visual changes)
To reviewers
zzzrenders fine throughformatDate(date-fnsformatsupports it), so users legitimately configure it indateFormat; only the parsing path threw. The try/catch also covers any other format-only tokenparserejects.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