Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assigning UTC timezone to UTC time shifts time as if original time was local #1801

Open
sulkaharo opened this issue Feb 9, 2022 · 0 comments

Comments

@sulkaharo
Copy link

sulkaharo commented Feb 9, 2022

Describe the bug

I'm creating a new dayjs object using an integer representing the millisecond time format for a moment in time. Outputting the object, the resulting date is correct and it has no time zone, but based on output it is in UTC as expected (human readable date ends in Z). If I then use the time zone plugin and adjust the time to UTC, the time is shifted as if the original time was in the local time of the machine running the code and not in UTC.

Expected behavior

Based on the string output of the original time, I'm expecting when assigning the UTC time zone to a dayjs object, the point in time is not shifted, but the presentation of the date might change to reflect the time zone.

The way Moment.js behaves in this case is, when creating the Moment object from the integer, the integer is interpreted as being in UTC, but the object is assigned the local time zone. When subsequently assigning the UTC zone to the Moment object, the moment in time remains constant, but the representation is shifted to UTC.

Example:

const d = dayjs(1434844800000);
const m = moment(1434844800000);

// d is now 2015-06-21T00:00:00.000Z with no zone in the dayjs object
// m is now 2015-06-21T03:00:00+03:00
// Note these dates are equal in terms of the moment in time

const zd = d.tz('UTC');
const zm = m.tz('UTC');

// zd is now 2015-06-20T21:00:00.000Z
// zm is now 2015-06-21T00:00:00Z
// Moment date is unchanged as a point in time, Dayjs time is shifted 3 hours

The logic I have that I'm porting has a persisted timestamp in UTC and code needs to know what time it was in a random time zone at that moment in time.

Information

  • Day.js Version 1.10.7
  • OS: Mac OS X 12.2
  • Node.js 16.13.2
  • Time zone: GMT+3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant