iamkun / iamkun/dayjs

add() result is wrong with timezone

Open
#2,598 2 comments 2 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 a dayjs object have a `Europe/London` timezone, adding time to it give a wrong result.

reproduction:
```js
import dayjs from "dayjs";
import utc from "dayjs/plugin/utc";
import timezone from "dayjs/plugin/timezone";

dayjs.extend(utc);
dayjs.extend(timezone);

const t = dayjs(1709695253000).tz("Europe/London");
const t1 = t.add(1, "hour");

console.log(t.valueOf(), t.utcOffset(), t.format()); // 1709695253000 0 2024-03-06T03:20:53Z
console.log(t1.valueOf(), t1.utcOffset(), t1.format()); // 1709698853000 0 2024-03-05T20:20:53Z
```
> https://codesandbox.io/p/sandbox/dayjs-bug-29pl4w?file=%2Fsrc%2Findex.mjs%3A4%2C1

The `add 1 hour` result is less than the original time! It is wrong!

Other timezone with offset "+00:00" like `Etc/UTC` and `Etc/Zulu` can also reproduce this bug.

**Expected behavior**
add() result should be correct. In this demo, t1.format() should be "2024-03-06T04:20:53Z"

**Information**
- Day.js Version 1.11.10
- OS: MacOS
- Browser: chrome 121.0.6167.160
- My Computer time zone: (UTC+08:00) Asia/Shanghai

Contributor guide

Open the contributing guide

Research direction

Start with the utc and timezone plugins and the add() call shown in the reproduction, then run the supplied Europe/London example to observe the incorrect value and format. Done means adding one hour produces the expected 2024-03-06T04:20:53Z result without decreasing the original timestamp.

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
Clearly specified
Newbie friendliness
50/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.