Fix: iOS/Safari date parsing RangeError - #35
Open
sentry[bot] wants to merge 1 commit into
Open
Conversation
✅ Deploy Preview for studenthub-student ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
|
|
||
| export function dateTimeFormat(value: string, dateFormat: string): string { | ||
| return format(value, dateFormat, { locale: i18n.language == 'en' ? enUS : ar }) | ||
| return format(toDate(value) as Date, dateFormat, { locale: i18n.language == 'en' ? enUS : ar }) |
Author
There was a problem hiding this comment.
Bug: The toDate() function incorrectly replaces all dashes with slashes, corrupting ISO 8601 date strings. This causes new Date() to return Invalid Date, leading to a crash.
Severity: CRITICAL
Suggested Fix
Modify the toDate() function to correctly handle ISO 8601 datetime strings. Instead of globally replacing dashes with slashes, pass the ISO string directly to new Date(), as it can parse this format natively. Remove the replace(/-/g, '/') call.
Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent. Verify if this is a real issue. If it is, propose a fix; if not, explain why it's
not valid.
Location: src/utils/common.ts#L19
Potential issue: The `toDate()` function at `src/utils/common.ts:19` uses
`date.replace(/-/g, '/')` to format date strings. This logic incorrectly transforms full
ISO 8601 datetime strings (e.g., from `date.toISOString()`) into an invalid format like
`2024/01/15T10:30:00.000Z`. When this invalid string is passed to `new Date()`, it
returns `Invalid Date`. This value is then used by `date-fns format()`, which throws a
`RangeError: Invalid time value`, leading to an application crash. This affects multiple
components that pass ISO strings to `dateTimeFormat`, such as `date-dropdown.tsx` and
`form-datetime.tsx`.
Also affects:
date-dropdown.tsx:35form-datetime.tsx:59
Did we get this right? 👍 / 👎 to inform future reviews.
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.
This PR addresses a
RangeError: Invalid time valueoccurring on iOS/WebKit browsers whendateTimeFormat()attempts to format date strings. The root cause was thatdateTimeFormat()was passing raw dash-delimited date strings (e.g., 'YYYY-MM-DD') directly todate-fns format(). Internally,date-fnsusesnew Date(), which on iOS/WebKit, cannot reliably parse dash-delimited strings, leading to the error.The fix involves wrapping the
valueparameter with the existingtoDate()helper function withindateTimeFormat(). ThetoDate()function correctly converts dash-delimited strings to a Safari-compatible format (replacing dashes with slashes) before they are passed todate-fns format(). This ensures that dates are parsed correctly across all browsers, resolving the crash on iOS devices.Fixes SH-STUDENT-APP-W
Fixes TECH-2019