iamkun / dayjs

⏰ Day.js 2kB immutable date-time library alternative to Moment.js with the same modern API
https://day.js.org
MIT License
46.62k stars 2.28k forks source link

`.valueOf` does not work properly with `utc` and `timezone` plugins #2533

Open fiarabbit opened 8 months ago

fiarabbit commented 8 months ago

Describe the bug When NodeJS is configured The combination of utc and timezone plugins does not handle .valueOf function properly

Code to reproduce

process.env.TZ = 'Europe/Amsterdam';

await import('dayjs').then(m => {dayjs = m.default;});
await import('dayjs/plugin/arraySupport.js').then(m => {arraySupport = m.default;});
await import('dayjs/plugin/timezone.js').then(m => {timezone = m.default;});
await import('dayjs/plugin/utc.js').then(m => {utc = m.default;});

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

var withoutTimezone = dayjs.utc([2021, 1, 26, 19, 13, 55, 751]);
console.log(withoutTimezone.format()); // 2021-02-26T19:13:55Z
var tenHoursBefore = withoutTimezone.clone().utcOffset(10, true);
console.log(tenHoursBefore.format()); // 2021-02-26T19:13:55+10:00

var diffInHours = (withoutTimezone.valueOf() - tenHoursBefore.valueOf()) / (1000 * 60 * 60);
console.log(diffInHours); // Error: 9 hours, Expected: 10 hours

Expected behavior When utc.utcOffset returns dayJs object without local host machine's timezone.

Information

fiarabbit commented 8 months ago

maybe fixed by https://github.com/iamkun/dayjs/pull/2532