fix: diff(year/month/quarter) returns NaN instead of 0 for Invalid Date - #3187
Open
koding88 wants to merge 3 commits into
Open
fix: diff(year/month/quarter) returns NaN instead of 0 for Invalid Date#3187koding88 wants to merge 3 commits into
koding88 wants to merge 3 commits into
Conversation
prettyUnit consulted the special map before lowercasing, then
stripped a trailing 's', so 'MS'/'Ms'/'mS' resolved to the string
'm'. Consequences while every other unit was case-insensitive:
- get('MS') threw TypeError (this.m is not a function)
- set('MS', n) was a silent no-op
- startOf/endOf('MS') were clone-only no-ops
Look up the special map again after lowercasing so mixed-case ms
behaves exactly like 'ms'.
monthDiff had `|| 0` which converted NaN to 0 when either operand was an Invalid Date. Replaced with `+ 0` to normalize -0 to +0 while letting NaN propagate, matching the behavior of all other diff units. Fixes iamkun#3186
There was a problem hiding this comment.
🟡 Changes recommended
The new Millisecond test compares two separate dayjs() instances and can be time-dependent/flaky; it should be made deterministic before merge.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Pull request overview
This PR fixes Day.js .diff() behavior for year/month/quarter units so that comparisons involving an Invalid Date propagate NaN (instead of being silently coerced to 0) while still normalizing -0 to +0 for valid calculations.
Changes:
- Updated
monthDiffto preserveNaNresults by removing the|| 0fallback and normalizing-0via+ 0. - Expanded
prettyUnitto recognize mixed-case millisecond unit inputs (e.g.,MS,Ms,mS) and added related tests. - Added regression tests for issue #3186 covering invalid-date operand ordering and a moment.js comparison for valid dates.
File summaries
| File | Description |
|---|---|
| src/utils.js | Fixes monthDiff NaN propagation and improves unit normalization in prettyUnit. |
| test/issues/issue3186.test.js | Adds coverage for NaN propagation in diff() for year/month/quarter with invalid dates and a valid-date regression check. |
| test/utils.test.js | Extends prettyUnit tests to validate mixed-case millisecond unit inputs. |
| test/get-set.test.js | Adds get/set/startOf tests for mixed-case millisecond unit inputs. |
Review details
- Files reviewed: 4/4 changed files
- Comments generated: 1
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| expect(dayjs().millisecond()).toBe(moment().millisecond()) | ||
| expect(dayjs().millisecond(0).valueOf()).toBe(moment().millisecond(0).valueOf()) | ||
| expect(dayjs().millisecond(1).valueOf()).toBe(moment().millisecond(1).valueOf()) | ||
| expect(dayjs().get('MS')).toBe(dayjs().get('ms')) |
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.
Fixes #3186.
monthDiffinsrc/utils.jsended with|| 0, which converted theNaNproduced by Invalid Date operands into a plausible-looking0. As a result,.diff(other, 'year'|'month'|'quarter', true)silently reported "0 units apart" for a meaningless comparison, while every other unit (day,hour,minute,second,week) correctly returnedNaNfrom the raw millisecond difference.Replaced
|| 0with+ 0to normalize-0to+0(the original purpose of the fallback) while lettingNaNpropagate.Added test covering year/month/quarter for both operand orderings, plus a regression check against moment.js for valid dates. All 798 tests pass, lint clean.