Skip to content

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

@lanistor

Description

@lanistor

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

  • Day.js Version: [ v1.11.10 ~ v1.11.13]
  • OS: [MacOS]
  • Browser [Node 18.19]
  • Time zone: [ GMT+08:00 (Asia/Shanghai)]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions