Fix: RangeError 'Invalid time value' in Safari date parsing - #36
Open
sentry[bot] wants to merge 1 commit into
Open
Fix: RangeError 'Invalid time value' in Safari date parsing#36sentry[bot] wants to merge 1 commit into
sentry[bot] wants to merge 1 commit into
Conversation
✅ Deploy Preview for studenthub-student ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
| import { enUS, ar } from "date-fns/locale"; | ||
| import i18n from "@/18n"; | ||
| import { format } from "date-fns"; | ||
| import { format, isValid } from "date-fns"; |
Author
There was a problem hiding this comment.
Bug: The toDate() function's global dash replacement corrupts ISO 8601 timestamps, causing new Date() to fail and downstream date formatting to return an empty string.
Severity: HIGH
Suggested Fix
Modify toDate() to avoid corrupting ISO 8601 timestamps. Instead of a global replacement, consider splitting the string by 'T', replacing dashes only in the date part, and then rejoining. Alternatively, use a more robust date parsing library or method that correctly handles ISO strings without manual manipulation.
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#L6
Potential issue: The `toDate()` function globally replaces all dashes with slashes. When
called with an ISO 8601 string containing a 'T' separator (e.g., from
`Date.toISOString()`), it produces an invalid date format like
"2024/01/01T09:30:00.000Z". This string cannot be parsed by `new Date()`, leading to an
`Invalid Date` object. Consequently, `dateTimeFormat()` returns an empty string, causing
UI components like date pickers in `form-datetime.tsx` and `date-dropdown.tsx` to
display blank values.
Also affects:
form-datetime.tsx:59date-dropdown.tsx:35
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 the
RangeError: Invalid time valueoccurring specifically on iPhone/Mobile Safari when displaying jobavailable_from/available_todates.Root Cause:
Safari's JavaScript engine cannot parse date strings containing dashes (e.g., '2024-01-01'), resulting in an
Invalid Dateobject. ThedateTimeFormat()function insrc/utils/common.tswas directly passing these raw, dash-separated strings todate-fns'sformat()function. Whenformat()receives anInvalid Date, it throws the observedRangeError.Solution:
dateTimeFormat()to utilize the existingtoDate()helper function (also insrc/utils/common.ts).toDate()correctly converts dash-separated date strings to slash-separated ones (e.g., '2024/01/01'), which Safari can parse.date-fns'sisValid()function. If, after conversion, the date is still invalid,dateTimeFormat()will now return an empty string, preventing theRangeErrorfrom being thrown and ensuring a graceful fallback.Fixes SH-STUDENT-APP-W
Fixes TECH-2019