fix(customParseFormat): preserve UTC mode when format is an array#3023
Open
Jadu07 wants to merge 2 commits intoiamkun:devfrom
Open
fix(customParseFormat): preserve UTC mode when format is an array#3023Jadu07 wants to merge 2 commits intoiamkun:devfrom
Jadu07 wants to merge 2 commits intoiamkun:devfrom
Conversation
When dayjs.utc(date, [...formats]) was called with an array of formats, dates were silently parsed in local time instead of UTC. The array branch was calling d.apply() (the default dayjs constructor) which has no awareness of the utc:true flag. Fix: after a valid format is found, recompute this.$d via parseFormattedInput() with the utc flag — matching the behaviour of the single-format code path. Fixes: #<issue-number>
Author
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 this PR does
Fixes #3013 —
dayjs.utc()ignores UTC mode when the format argument is an array.The bug
If you pass an array of formats to
dayjs.utc()(via thecustomParseFormatplugin), the date gets parsed in local time instead of UTC. No error is thrown, which makes the bug really hard to spot — your tests might pass fine inTZ=UTCbut silently break everywhere else.Why it happened
In
customParseFormat/index.js, theformat instanceof Arraybranch loops through each format and tries it withd.apply(this, args). The problem is thatd.applyjust calls the regulardayjs()constructor, which has no idea aboututc: true. So every format attempt ends up using local time.Meanwhile, the single-format code path calls
parseFormattedInput(date, format, utc, d), which correctly threads theutcflag through.The fix
One line change — once we find a valid format, compute
this.$dusingparseFormattedInputwith theutcflag instead of blindly copyingresult.$d:if (result.isValid()) { - this.$d = result.$d + this.$d = utc + ? parseFormattedInput(date, format[i - 1], utc, d) + : result.$dTests
Added two regression tests to
test/plugin/utc.test.js:Full suite: 774 tests, 93 suites — all passing.
npm run lintis clean too.