iamkun / iamkun/dayjs

`toDate` shifts actual date by 1 hour

Open
#1,791 2 comments 1 reaction 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**
`toDate()` shifts time by 1 hour. This only happens when I am shifting from summertime (October 31 03:00:00 2021 GMT +2) to wintertime (October 31 02:00:00 2021 GMT +1) in **runtime**.

The problem stops persisting in runtime at **2021-11-02 02:16:05 +0100**.

**Current time:** Sun Oct 31 2021 02:00:03 GMT+0100 (Central European Standard Time)
**Input:** 21-02-18 00:00:00

```js
const dayjsResult = dayjs(input, format).tz(timeZone); // Wed Feb 21 2018 00:00:00 GMT+0100 (Central European Standard Time)
dayjsResult.toDate(); // Tue Feb 20 2018 23:00:00 GMT+0100 (Central European Standard Time)
```

**Expected behavior**
`toDate()` should have not shifted hours, but instead keep the same datetime

**Work around**
```js
const dayjsResult = dayjs('21-02-18 00:00:00', 'DD-MM-YY HH:mm:ss').tz('Europe/Amsterdam');
const correctDate = new Date(dayjsResult.$d); // Wed Feb 21 2018 00:00:00 GMT+0100 (Central European Standard Time)
```

I am not sure where the core issue lies and why using the internal [`getTime()`](https://github.com/iamkun/dayjs/blob/06f88f425828b1ce96b737332d25145a95a4ee9d/src/index.js#L134) would not be enough for this.

**Information**
- Day.js Version 1.10.4 / 1.10.7
- Browser Node 16.13.2
- Time zone: GMT +1 / GTM +2

**Related**
https://github.com/iamkun/dayjs/issues/1805

Contributor guide

Open the contributing guide

Research direction

Start in src/index.js at the linked getTime() location and reproduce the daylight-saving transition with the supplied parsing, timezone, and toDate() example. Compare the result with the documented new Date(dayjsResult.$d) workaround. Done means toDate() preserves the expected local datetime during the reported summer-to-winter transition.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
web-dev
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 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.