iamkun / iamkun/dayjs

utcOffset() does not respect .tz()

Open
#2,405 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
.utc('2020/12/30 12:00', 'YYYY/MM/DD hh:mm', true)
.tz('America/New_York');
const offset = dayjsObj.utcOffset();
console.log({
dayjsObj,
'dayjsObj.format()': dayjsObj.format(),
offset,
dayjsObj.utcOffset(offset)': dayjsObj.utcOffset(offset),
'dayjsObj.utcOffset(offset).format()': dayjsObj
.utcOffset(offset)
.format(),
'dayjsObj.local().format()': dayjsObj.local().format(),
'dayjsObj.local()': dayjsObj.local(),
});
```
gives inconsistent output. On a computer where `dayjsObj.local().format()` is `2020-12-30T12:00:00+00:00`, `dayjsObj.utcOffset(offset).format()` gives `2020-12-30T12:00:00-05:00`:
```
{
dayjsObj: M {
'$L': 'en',
'$u': false,
'$d': 2020-12-30T07:00:00.000Z,
'$x': { '$timezone': 'America/New_York' },
'$y': 2020,
'$M': 11,
'$D': 30,
'$W': 3,
'$H': 7,
'$m': 0,
'$s': 0,
'$ms': 0,
'$offset': -300
},
'dayjsObj.format()': '2020-12-30T07:00:00-05:00',
offset: -300,
'dayjsObj.utcOffset(offset)': M {
'$L': 'en',
'$u': false,
'$d': 2020-12-30T12:00:00.000Z,
'$x': { '$localOffset': 300 },
'$y': 2020,
'$M': 11,
'$D': 30,
'$W': 3,
'$H': 12,
'$m': 0,
'$s': 0,
'$ms': 0,
'$offset': -300
},
'dayjsObj.utcOffset(offset).format()': '2020-12-30T12:00:00-05:00',
'dayjsObj.local().format()': '2020-12-30T12:00:00+00:00',
'dayjsObj.local()': M {
'$L': 'en',
'$u': false,
'$d': 2020-12-30T12:00:00.000Z,
'$x': {},
'$y': 2020,
'$M': 11,
'$D': 30,
'$W': 3,
'$H': 12,
'$m': 0,
'$s': 0,
'$ms': 0
}
}
```
On another computer where `dayjsObj.local().format()` is `2020-12-30T07:00:00-05:00`, `dayjsObj.utcOffset(offset).format()` gives `2020-12-30T07:00:00-05:00`:
```
{
dayjsObj: M {
'$L': 'en',
'$u': false,
'$d': 2020-12-30T12:00:00.000Z,
'$x': { '$timezone': 'America/New_York' },
'$y': 2020,
'$M': 11,
'$D': 30,
'$W': 3,
'$H': 7,
'$m': 0,
'$s': 0,
'$ms': 0,
'$offset': -300
},
'dayjsObj.format()': '2020-12-30T07:00:00-05:00',
offset: -300,
'dayjsObj.utcOffset(offset)': M {
'$L': 'en',
'$u': false,
'$d': 2020-12-30T12:00:00.000Z,
'$x': { '$localOffset': 300 },
'$y': 2020,
'$M': 11,
'$D': 30,
'$W': 3,
'$H': 7,
'$m': 0,
'$s': 0,
'$ms': 0,
'$offset': -300
},
'dayjsObj.utcOffset(offset).format()': '2020-12-30T07:00:00-05:00',
'dayjsObj.local().format()': '2020-12-30T07:00:00-05:00',
'dayjsObj.local()': M {
'$L': 'en',
'$u': false,
'$d': 2020-12-30T12:00:00.000Z,
'$x': {},
'$y': 2020,
'$M': 11,
'$D': 30,
'$W': 3,
'$H': 7,
'$m': 0,
'$s': 0,
'$ms': 0
}
}
```
Despite `dayjsObj` being set to the same timezone (`America/New_York`), `utcOffset` ignores it and uses the `local` time instead. I believe this is the result of the following line of code:

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

Is the intended behavior to use the local time regardless of the timezone that was set, or is it something to be fixed to respect the converted timezone?

Contributor guide

Open the contributing guide

Research direction

Start at src/plugin/utc/index.js around the linked line 99 and reproduce the supplied utc/tz example on both local-time environments. Determine the intended utcOffset behavior after tz('America/New_York'); done means the behavior is resolved and the reported inconsistency no longer occurs.

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.