Setting identical timezone to the current timezone subtracts an hour
- Dominant language
- JavaScript
- Stars
- 48.7k
- Forks
- 2.5k
- PR merge metrics
- No merged PRs in 30d
Description
**Describe the bug**
In the timezone plugin, setting a timezone to a date can result in subtracting an hour when it wasn't supposed to be.
```
const today = dayjs('2021-10-10T23:00:00.000Z').tz('America/Los_Angeles') // converts to 4pm
const todayPacific = dayjs(today).tz('America/Los_Angeles') // converts to 3pm
```
The culprit is this line: https://github.com/iamkun/dayjs/blob/dev/src/plugin/timezone/index.js#L98
Doing `const target = date.toLocalString('en-US', { timeZone: timezone})` will return a string _without_ any timezone data so when the code executes the next line with `new Date(target)`, that date object is no longer in the correct timezone.
Recreated this behavior here:
https://codesandbox.io/s/damp-cherry-lzv2z?file=/src/App.js
I inspected the objects -- they both are set in the correct timezone for the date (`PDT`) with identical offset but their Unix timestamps are off. That shouldn't happen when setting timezone data.
**Expected behavior**
Setting an identical timezone to a date should not change the time.
**Information**
- Day.js Version 1.10.7
- OS: MacOS Big Sur
- Browser: Chrome 95
- Time zone: GMT-07:00 DST (Pacific Daylight Time)
Contributor guide
Research direction
Start in src/plugin/timezone/index.js at the linked line and reproduce the behavior with the issue's Day.js example or CodeSandbox. Trace the local-time conversion and verify that applying the same America/Los_Angeles timezone preserves the original Unix timestamp while retaining the correct PDT offset.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript
- Domain
- localization
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 48/100