iamkun / iamkun/dayjs

.add or .subtract messes up an isBefore test on dayjs.tz objects

Open
#2,339 0 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

**Describe the bug**
If doing an isBefore comparison and using two dayjs objects that are both created with the timezone extension. isBefore returns False when it should be True if doing a .add or .substract first, which seems to mess everything up. Note that if you add or substract on the second object, it works fine. Check out the demo below!

**Expected behavior**
A date that is before another should return True

**Information**
- Day.js Version 1.11.7
- OS: Win11
- Browser Edge 114.0.1823.43
- Time zone: [America/Boise (GMT-7:00)]

Example (note that add and subtract return a cloned dayjs object and don't modify the originals):

```
import dayjs from 'dayjs'
import utc from 'dayjs/plugin/utc'
import tz from 'dayjs/plugin/timezone'
import customParseFormat from 'dayjs/plugin/customParseFormat'
dayjs.extend(customParseFormat)
dayjs.extend(utc)
dayjs.extend(tz)
let date1 = dayjs.tz(dayjs.utc(), 'Africa/Abidjan')
let date2 = dayjs.tz(dayjs.utc().add(1, 'minute'), 'Africa/Abidjan')

console.log('initial diff: ' + date2.diff(date1)) // 60000
console.log('diff after adding a minute', date2.add(1, 'minute').diff(date1)) // 120000
console.log('correct', date2.isBefore(date1)) // false
console.log('correct', date2.isBefore(date1.add(5, 'minute'))) // true
console.log('incorrect!', date2.subtract(5, 'minute').isBefore(date1)) // false
```

Contributor guide

Open the contributing guide

Research direction

Start by running the supplied JavaScript reproduction with utc, timezone, and customParseFormat enabled. Trace isBefore after add and subtract on the timezone-created objects; done means the shown comparisons return the expected boolean values while the original objects remain unchanged.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
web-dev
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.