Dayjs.diff appears to malfunction when the dates have different time zones
- 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
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