iamkun / iamkun/dayjs

dayjs.tz returns different values depending on the type of input

Open
#2,680 0 comments 1 reaction 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**
`dayjs.tz(instance, timezone)` returns different values depending on the type of input. If the `instance` is of type string, then the value returned is correct and as per the documentation: it sets the time in the specified timezone. But if it is anything else, it doesn't work properly: it sets the time in the local timezone, then updates the timezone to the specified timezone.

For example, if this is the input:
```ts
// local timezone: UTC + 5:30

const tz = 'Asia/Tokyo'; // UTC + 9
const date = new Date(2023, 9, 8, 0, 0, 0, 0);

const a = dayjs.tz('2023-10-08', tz);
const b = dayjs.tz(date, tz);
const c = dayjs.tz(date.valueOf(), tz);
const d = dayjs.tz({year: 2023, month: 9, day: 8}, tz);

console.info('test a', a.toISOString(), a.format());
console.info('test b', b.toISOString(), b.format());
console.info('test c', c.toISOString(), c.format());
console.info('test d', d.toISOString(), d.format());

```

then this is the output:
```
test a 2023-10-07T15:00:00.000Z 2023-10-08T00:00:00+09:00
test b 2023-10-07T18:30:00.000Z 2023-10-08T03:30:00+09:00
test c 2023-10-07T18:30:00.000Z 2023-10-08T03:30:00+09:00
test d 2023-10-07T18:30:00.000Z 2023-10-08T03:30:00+09:00
```

**Expected behavior**
The output should be the same for all types of instances.

**Information**
- Day.js Version: v1.11.11
- OS: Linux
- Browser: Firefox 126.0.1
- Time zone: UTC + 5:30

Contributor guide

Open the contributing guide

Research direction

Start at the dayjs.tz entry point and reproduce the documented examples with the string, Date, timestamp, and object inputs in a UTC+5:30 environment. Compare the formatted and ISO outputs for each input; done means all four inputs produce the same timezone-adjusted value.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
web-dev
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.