Skip to content

fix: diff(year/month/quarter) returns NaN instead of 0 for Invalid Date - #3187

Open
koding88 wants to merge 3 commits into
iamkun:devfrom
koding88:fix/diff-nan-invalid-date
Open

fix: diff(year/month/quarter) returns NaN instead of 0 for Invalid Date#3187
koding88 wants to merge 3 commits into
iamkun:devfrom
koding88:fix/diff-nan-invalid-date

Conversation

@koding88

Copy link
Copy Markdown

Fixes #3186.

monthDiff in src/utils.js ended with || 0, which converted the NaN produced by Invalid Date operands into a plausible-looking 0. 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 returned NaN from the raw millisecond difference.

Replaced || 0 with + 0 to normalize -0 to +0 (the original purpose of the fallback) while letting NaN propagate.

dayjs('not a date').diff(dayjs(), 'month', true) // 0 → NaN
dayjs('not a date').diff(dayjs(), 'year', true)  // 0 → NaN
dayjs('not a date').diff(dayjs(), 'day', true)   // NaN (unchanged)

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.

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
Copilot AI lite review requested due to automatic review settings August 26, 2026 03:29

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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 monthDiff to preserve NaN results by removing the || 0 fallback and normalizing -0 via + 0.
  • Expanded prettyUnit to 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.

Comment thread test/get-set.test.js Outdated
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'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

diff(..., 'year'|'month', true) returns 0 instead of NaN for an Invalid Date operand

2 participants