iamkun / iamkun/dayjs

toISOString() is inconsistent with set timezone

Open
#2,408 1 comment 6 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
48.7k
Forks
2.5k
PR merge metrics
No merged PRs in 30d

Description

Running
```
const dayjsObj = dayjs('2020-01-02T15:14:59.741774+00:00').tz('America/New_York', true);
console.log(dayjsObj.toISOString());
```
gives inconsistent results depending on the timezone the computer is running it in. For example, a computer in the `America/New_York` timezone would log out `2020-01-02T15:14:59.741Z` whereas a computer in `UTC` would log out `2020-01-02T20:14:59.741Z`.

I believe this is due to the following line of code:

https://github.com/iamkun/dayjs/blob/a9d7d0398d22ebd4bfc3812ca0134a97606d54d9/src/plugin/utc/index.js#L117

For the computer in `America/New_York`, running `new Date().getTimezoneOffset()` gives `300` and for the computer in `UTC`, it would give `0`. In both computers, `dayjsObj.$offset` is `-300` and there is no `dayjsObj.$x.$localOffset`. As a result, `addedOffset` would be `0` for the computer in `America/New_York` but `-300` for the computer in `UTC`. This `addedOffset` would be used to create a new `Date` object in which the ISO string is derived from.

Is this intended behavior or should the `dayjsObj` provide the same ISO string regardless of the timezone a computer is running in?

Contributor guide

Open the contributing guide

Research direction

Start with the reproduction in the issue and inspect src/plugin/utc/index.js at the linked line, focusing on how addedOffset is calculated from $offset and the host timezone. Compare toISOString() output under America/New_York and UTC, then determine and document the intended consistent behavior and the verification needed for it.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
api
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.