fix(date): parse d/m/Y format and avoid mangling ISO offset strings in toDate - #2668
Merged
Conversation
🦋 Changeset detectedLatest commit: b822462 The changes in this PR will be included in the next version bump. This PR includes changesets to release 87 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
@hsngdz is attempting to deploy a commit to the Reach Digital Team on Vercel. A member of the Team first needs to authorize it. |
Member
|
Nice! |
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
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.
What
Fixes two parsing bugs in
toDate.Why
2024-01-15T10:30:00-05:00) were silently corrupted intoInvalid Date. The original code ranreplace(/-/g, '/')on every string, not just plainYYYY-MM-DDdates, so the offset's hyphen got mangled too.DATETIME_SLASH_PHP_FORMAT(d/m/Y H:i:s) were misparsed by the nativeDateconstructor, which assumesMM/DD/YYYYfor slash-separated strings. This produced either the wrong date (for ambiguous values like01/02/2024) orundefined(for unambiguous ones like15/01/2024).Changes
replace(/-/g, '/')string-hack entirely with explicit regex parsing + numericDateconstruction (fromComponents) for bothYYYY-MM-DDandd/m/Y H:i:sformatsd/m/Y H:i:sparsing matches Magento's confirmed day-first format (DATETIME_SLASH_PHP_FORMAT)fromComponentsalso round-trips the constructed date against its inputs, so out-of-range values (e.g.31/02/2026) are correctly rejected instead of being silently normalized by JS into a different, valid-looking dateTest plan
Dateinstances (valid/invalid), numeric timestamps,YYYY-MM-DD, ISO with offset (regression case),d/m/Y H:i:swith/without time, ambiguousd/m/Yvalues, invalid strings, and invalid calendar dates