iamkun / dayjs

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

utcOffset(0, true) with set('hour', n) bug, get wrong time. #2754

Open lanistor opened 1 month ago

lanistor commented 1 month ago

Describe the bug Try this in GMT+0800 timezone:

dayjs('2024-10-20T01:00:00').utc().utcOffset(0, true).set('hour', 2);
  // -> Sat, 19 Oct 2024 10:00:00 GMT
dayjs('2024-10-20T01:00:00').utc().utcOffset(0, true);
  // -> Sun, 20 Oct 2024 01:00:00 GMT
dayjs('2024-10-20T01:00:00').utc().utcOffset(0, true).add(1, 'hour');
  // -> Sun, 20 Oct 2024 02:00:00 GMT

// this also have wrong behavior
dayjs('2024-10-20T01:00:00').utcOffset(0, true).set('hour', 2);
  // -> Sat, 19 Oct 2024 10:00:00 GMT
dayjs('2024-10-20T01:00:00').utc(true).utcOffset(0, true).add(1, 'hour');
  // -> Sun, 20 Oct 2024 10:00:00 GMT
截屏2024-10-22 00 45 56

The aim is to turn to user's timezone and then change hour or date, the timezone may be 0 ~12.

Expected behavior dayjs('2024-10-20T01:00:00').utc().utcOffset(0, true).set('hour', 2); should get Sun, 20 Oct 2024 02:00:00 GMT;

It seem because the utcOffset method doesn't change the $d.

Information

josephmathew900 commented 3 days ago

https://joseph-mathew.neetorecord.com/watch/03fcfa68-ba46-4aa7-8fc8-aabb8eace0ee

@iamkun Could you please check this issue? The UTC logic for the timezone plugin is added in PR https://github.com/iamkun/dayjs/pull/2693.