iamkun / iamkun/dayjs

Timezone bug: wrong behavior with strings containing timezone infos compared to momentjs

Open
#2,466 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**
The timezone plugin behaves wrong compared to the respective `moment` plugin.

Example: I am working with an external API, which returns a date in a string format like this: `Wed, 16 Aug 2023 10:47:24 +0200`

As you can see, this string already contains a timezone information, in this case UTC+2.

We have a setup internally, where we always try to first parse any date in respect to the users timezone - in this case, the user, which is "connected" to this API call, is in the `Europe/Vienna` timezone --> UTC+2 is already the correct timezone.

Consider this code:

```js
moment.tz('Wed, 16 Aug 2023 10:47:24 +0200', 'Europe/Vienna').utc().format()
// returns: '2023-08-16T08:47:24Z'

// intentionally trying to pass a completely wrong timezone:
moment.tz('Wed, 16 Aug 2023 10:47:24 +0200', 'Asia/Taipei').utc().format()
// ALSO returns: '2023-08-16T08:47:24Z'

// WHY? because the "input string" already contains timezone info --> the 2nd param is kind of ignored and both lines of code return the same value.
// in my opinion this is the correct behavior, because why would moment consider the 2nd param, if the 1st param already contains proper timezone infos?

// dayjs:
dayjs.tz('Wed, 16 Aug 2023 10:47:24 +0200', 'Europe/Vienna').utc().format()
// returns: '2023-08-16T06:47:24Z'
// in my opinion this is wrong, because the "timezone conversion" actually happens twice, which does not make any sense to me.
```

This current behavior is really weird, because the moment vs. dayjs behavior is identical **if** the first param for `.tz()` is either a proper JS Date or a String WITHOUT timezone infos. But in this special case (a string WITH timezone infos), the behavior suddenly is really different. This resulted in a lot of wrong dates in our codebase since we switched from moment to dayjs.

**Expected behavior**
Calling `dayjs.tz()` with a string in a format already containing timezone infos, the 2nd param of `.tz()` should actually be ignored.

**Information**
- Day.js Version v1.11.9
- OS: nodejs
- Browser -
- Time zone: Vienna Austria

Contributor guide

Open the contributing guide

Research direction

Start at the timezone plugin entry point behind dayjs.tz and reproduce the supplied string-with-offset example in Node.js. Compare its output with moment.tz and the existing behavior for Date inputs and strings without timezone information. Done means timezone-bearing strings produce the expected UTC result without applying the supplied timezone twice.

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.