iamkun / iamkun/dayjs

Dayjs.diff appears to malfunction when the dates have different time zones

Open
#1,837 5 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
48.7k
Forks
2.5k
PR merge metrics
No merged PRs in 30d

Description

**Description**

When one of the dates has been altered with `tz`, calling `date.diff(otherDate, "day")` produces unexpected results:

```js
const dayjs = require("dayjs")
const utc = require("dayjs/plugin/utc")
const timezone = require("dayjs/plugin/timezone")
dayjs.extend(utc)
dayjs.extend(timezone)

const now = dayjs()

const works = now.add(121, "hour")
works.diff(now, "day") // 5 ✅

const doesntWork = now.tz("America/Chicago").add(121, "hour")
doesntWork.diff(now, "day") // 4 ❌

works.valueOf() === doesntWork.valueOf() // true
works.toISOString() === doesntWork.toISOString() // true
works.diff(now, "hour") === doesntWork.diff(now, "hour") // true (both 121)
```

**Expected behavior**

Since the dates are the same internally whether `tz` was called or not, `diff` should produce identical results.

**Information**
- Day.js version: 1.11.0
- OS: Ubuntu 20.04
- Node: 14.18.3

Contributor guide

Open the contributing guide

Research direction

Start with the minimal JavaScript reproduction using the utc and timezone plugins, then trace the diff and tz entry points. Compare day and hour results for the two equivalent dates; done means diff returns identical day results when valueOf() and toISOString() match.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.