iamkun / iamkun/dayjs

Timezone conversion to same timezone keeping time returns wrong valueOf

Open
#2,763 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**
When you convert a date to the same timezone and with the `keepLocalTime` set as `true` the `valueOf` function returns the wrong milis value.

See this code snippet:
```js
import dayjs from 'dayjs';
import timezone from 'dayjs/plugin/timezone';
import utc from 'dayjs/plugin/utc';

dayjs.extend(timezone);
dayjs.extend(utc);

// Update: Apparently it only happens when the timezone is UTC
const timezoneToUse = 'UTC'

// Set default timezone
dayjs.tz.setDefault(timezoneToUse);

const date1 = dayjs.tz('2021-08-09T12:45:00Z');
console.log(date1.format()); // 2021-08-09T12:45:00Z
console.log(date1.valueOf()); // 1628513100000

const date2 = dayjs.tz('2021-08-09T12:45:00Z').tz(timezoneToUse);
console.log(date2.format()); // 2021-08-09T12:45:00Z
console.log(date2.valueOf()); // 1628513100000

const date3 = dayjs.tz('2021-08-09T12:45:00Z').tz(timezoneToUse, true);
console.log(date3.format()); // 2021-08-09T12:45:00Z
console.log(date3.valueOf()); // 1628520300000 -> why?
```

**Expected behavior**
`valueOf` should return the same value regardless if you set `keepLocalTime` on or off if you are parsing from the same timezone.

**Information**
- Day.js Version v1.11.13
- OS: MacOS Sonoma 14.7
- Browser Chrome v129.0.6668.91
- Time zone: Europe/Prague

Contributor guide

Open the contributing guide

Research direction

Start with the timezone and utc plugin entry points shown in the reproduction, then run the provided UTC example to compare valueOf with and without keepLocalTime. Done means the same-timezone conversion returns the same valueOf result in both cases, while preserving the expected formatting.

Written by the indexing model from the issue text.

Assessment

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