iamkun / iamkun/dayjs

Adding days across daylight savings time in GMT timezone has unexpected result

Open
#2,238 7 comments 3 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**

I am using version 1.11.6 of dayjs.

I have a simple test case for what I think is a dayjs bug. I add 2 days to a day in GMT/UTC timezone and get something that is not on a day boundary. It probably has something to do with daylight savings time because this is when daylight savings time happens in US. However, why its it a 2 hour difference, and why does it happen in GMT/UTC time? This change did not happen when we used moment.js.
```
it('should be exactly two days later when adding two days', function () {
const day1 = dayjs.tz({ year: 2018, month: 2, day: 10 }, 'GMT');
const day2 = day1.add(2, 'day');
expect(day2.valueOf()).toEqual(dayjs.tz({ year: 2018, month: 2, day: 12, hour: 2 }, 'GMT').valueOf());
});
```

**Expected behavior**

This version of the test should pass. We should not need to add two hours to the expected result.

it('should be exactly two days later when adding two days', function () {
const day1 = dayjs.tz({ year: 2018, month: 2, day: 10 }, 'GMT');
const day2 = day1.add(2, 'day');
expect(day2.valueOf()).toEqual(dayjs.tz({ year: 2018, month: 2, day: 12 }, 'GMT').valueOf());
});

**Information**
- Day.js Version 1.11.6
- OS: IOS/mac
- Browser N/A
- Time zone: GMT. My local machine TZ is PST and that might affect the test result.

Contributor guide

Open the contributing guide

Research direction

Start by running the inline timezone test case from the issue with Day.js 1.11.6 and compare the result for GMT with the expected day boundary. Trace the timezone add behavior until the two-day assertion passes without adding two hours, then retain a regression test for the reported case.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.