iamkun / iamkun/dayjs

Converting a local time to UTC sometimes applies one extra hour

Open
#1,795 2 comments 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**

Converting a UTC time to a local time using `timezone` plugin seems to apply the difference in DST compare to the _current time_ when converting back to JSON.
So if I have a time expressed in Europe/Stockholm DTS time, and my machine is on Stockholm standard time, when converting the time to JSON, an extra hour seems to be subtracted.

Example:
Today is 2022-02-24, I'm in Europe/Stockholm which is currently on standard time, no daylight savings, so UTC+01:00.

```
import dayjs from 'dayjs';
import timezone from 'dayjs/plugin/timezone';
import utc from 'dayjs/plugin/utc';

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

const winterUtc = dayjs.utc('2022-03-01T15:00:00Z');
console.log('Winter UTC JSON:', winterUtc.toJSON());
const winterLocal = winterUtc.tz('Europe/Stockholm'); // 15.00 UTC is 16:00+01:00 in March
console.log('Winter Europe/Stockholm format:', winterLocal.format());
// prints 2022-03-01T16:00:00+01:00 👍
console.log('Winter Europe/Stockholm UTC offset:', winterLocal.utcOffset());
// prints 60 👍
console.log('Winter Europe/Stockholm JSON:', winterLocal.toJSON());
// prints 2022-03-01T15:00:00.000Z 👍

const summerUtc = dayjs.utc('2022-06-01T15:00:00Z');
console.log('Summer UTC JSON:', summerUtc.toJSON());
const summerLocal = summerUtc.tz('Europe/Stockholm'); // 15:00 UTC is 17:00+02:00 in June
console.log('Summer Europe/Stockholm format:', summerLocal.format());
// prints 2022-06-01T17:00:00+02:00 👍
console.log('Summer Europe/Stockholm UTC offset:', summerLocal.utcOffset());
// prints 120 👍
console.log('Summer Europe/Stockholm JSON:', summerLocal.toJSON());
// prints 2022-06-01T14:00:00.000Z, should be 2022-06-01T15:00:00.000Z 👎
```

It's unclear to me if that always happens or if it's due to my local machine being on standard time while I'm computing a date in DST, and if the opposite problem would happen if my local machine is on DST while computing a "winter time" date.

**Expected behavior**

A time expressed in a time zone should be possible to convert to UTC / JSON by applying the UTC offset regardless of the local machine's own time zone.

**Information**
- Day.js Version [e.g. v1.7.0]
- OS: [e.g. MacOS 12]
- Browser: N/A, Node 14
- Time zone: Europe/Stockholm, UTC+01:00

Contributor guide

Open the contributing guide

Research direction

Start with the timezone and utc plugins and reproduce the Stockholm winter and summer conversions from the issue. Compare the summer value returned by toJSON() with the expected UTC instant, and consider the issue complete when both seasonal examples preserve the correct instant regardless of the machine's local time zone.

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
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.