iamkun / iamkun/dayjs

Incorrect Format value in UTC

Open
#2,689 0 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**
When I create date, parse it to UTC, then add some duration to it I'm getting incorrect time value in .format('HH:mm')

```
const timeZone = 'UTC'; // Timezone is UTC
const start = '2022-11-04T06:00:00Z'; // some example date
const duration = 'PT11H'; // and duration

const startDateTime = dayjs(start).tz(timeZone); // create initial date
const finishDateTime = startDateTime.add(dayjs.duration(duration)); // add to initial date given duration, this will return incorrect time in format
const tempFinishDateTime = startDateTime
.add(dayjs.duration(duration))
.tz(timeZone); // to make format return correct value I have to parse again to UTC, no matter that startDateTime was already parsed to UTC

console.log('timeZone', timeZone);
console.log('finishDateTime', finishDateTime);// here hours is set to 16, should be 17
console.log('tempFinishDateTime', tempFinishDateTime);// here hours is correct
console.log('result.valueOf', finishDateTime.valueOf());
console.log('tempFinishDateTime.valueOF', tempFinishDateTime.valueOf());
console.log(
'isValueEqual',
finishDateTime.valueOf() === tempFinishDateTime.valueOf(),
); // most interesting is that valueOf are equals for both dates
console.log('finishDateTime.format', finishDateTime.format('HH:mm'));// return 16:00 which is incorrect
console.log('tempFinishDateTime.format', tempFinishDateTime.format('HH:mm')); // return 17:00 as it should be
```

![image](https://github.com/iamkun/dayjs/assets/11505490/a15c69a9-570b-48bd-a44b-45e0e3f1050e)

When I use other timezone, for example 'europe/zurich' everything works as expected.
![image](https://github.com/iamkun/dayjs/assets/11505490/95817dcb-492b-4562-b412-98d45e689996)

**Expected behavior**
I should not have to parse finish date to given timezone again, as start date was already parsed to selected timezone.

**Information**
- Day.js Version 1.11.11
- OS: macOS 14.5
- Browser chrome: 126.0.6478.62

Contributor guide

Open the contributing guide

Research direction

Reproduce the example using the timezone, duration, add, and format calls shown in the issue, starting with the Day.js timezone and duration behavior. Compare the formatted result before and after the second tz('UTC') call, then inspect the relevant plugin entry points. Done means adding a regression test and making the first format call produce 17:00 without reparsing.

Written by the indexing model from the issue text.

Assessment

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