iamkun / iamkun/dayjs

Dayjs utcOffset set twice will get incorrect utc offset.

Open
#1,643 0 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**
When I set the utcOffset at the second time, the time will be change to my local timezone.

**Expected behavior**
Timezone should be changed according to the setting

![image](https://user-images.githubusercontent.com/22948771/134488957-bad95571-8825-468d-8229-be899d76dea4.png)

My current timezone is +8
When I set a dayjs object with +7, it is working well.
2021-10-23 09:00 +7 is the expected result.
Then I want to have a +0 result, which ought to be 2021-10-23 02:00 +0
But I got '2021-10-23T10:00:00Z'

In moment.js , it is fine.
```
moment.utc("2021-10-23 09:00").utcOffset(7, true).utcOffset(0).format()
// '2021-10-23T02:00:00Z' -- ok
```

I think the 10AM came from 2+8 since my local timezone is +8

By now, how I solve it is formatting the object to string, when convert the string to object:
```
const a = dayjs.utc("2021-10-23 09:00").utcOffset(7, true).format()
// '2021-10-23T09:00:00+07:00'
const b = dayjs.utc(a).format()
// '2021-10-23T02:00:00Z' -- ok
```

**Information**
- Day.js Version [v1.10.4]
- OS: [iOS]
- Browser [chrome Version 93.0.4577.82 (Official Build) (x86_64)]
- Time zone: [GMT+0800 (China Standard Time)]

Thanks for your time!

Contributor guide

Open the contributing guide

Research direction

Start with the utcOffset call sequence in the reproduction and trace how the second offset is applied after utcOffset(7, true). Use the expected 2021-10-23T02:00:00Z result as the regression case, and verify that repeated offset changes preserve the intended instant and offset behavior.

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
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.