iamkun / iamkun/dayjs

Setting Timezone adds an extra hour

Open
#1,664 1 comment 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**
There seems to be an extra hour being added when setting timezones:

```js
dayjs.utc('2021-01-01').toString() // 'Fri, 01 Jan 2021 00:00:00 GMT'
dayjs.utc('2021-01-01').tz('America/Chicago').toString() // 'Fri, 01 Jan 2021 01:00:00 GMT'
dayjs.utc('2021-01-01').tz('America/Chicago', false).toString() // 'Fri, 01 Jan 2021 01:00:00 GMT'
dayjs.utc('2021-01-01').tz('America/Chicago', true).toString() // 'Fri, 01 Jan 2021 07:00:00 GMT' should be 06
```
Seems to be related to daylight savings time, as it only appears half the year:
```js
dayjs.utc('2021-01-01').tz('America/Chicago')
.tz('America/Chicago')
.tz('America/Chicago')
.tz('America/Chicago').toString() // 'Fri, 01 Jan 2021 04:00:00 GMT'
dayjs.utc('2021-07-01').tz('America/Chicago')
.tz('America/Chicago')
.tz('America/Chicago')
.tz('America/Chicago').toString() // 'Thu, 01 Jul 2021 00:00:00 GMT'
```

**Expected behavior**

That it works like momentjs:

```js
dayjs('2021-01-01').tz('America/Chicago').toISOString() // '2021-01-01T07:00:00.000Z'
moment('2021-01-01').tz('America/Chicago').toISOString() // '2021-01-01T06:00:00.000Z'
```

**Information**
- Day.js Version [v1.10.7, from `day.js.org`]
- OS: [Linux/Ubuntu]
- Browser [Chrome Version 94.0.4606.81 (Official Build) (64-bit)]
- Time zone: [GMT-06:00 DST (Central Daylight Time)]

Contributor guide

Open the contributing guide

Research direction

Start by running the reported dayjs.utc(...).tz('America/Chicago') examples from the issue and compare their output with the expected Moment.js results. Trace the .tz entry point and its daylight-saving offset handling, then add or update a regression test covering the January and July cases. Done means repeated timezone conversion no longer adds an hour and the shown expectations pass.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.