Assigning UTC timezone to UTC time shifts time as if original time was local
- Dominant language
- JavaScript
- Stars
- 48.7k
- Forks
- 2.5k
- PR merge metrics
- No merged PRs in 30d
Description
**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
Contributor guide
Research direction
Start by reproducing the provided millisecond example with the timezone plugin in a GMT+3 environment, comparing d.tz('UTC') with Moment's result. Trace the timezone conversion path and add regression coverage showing that assigning UTC preserves the instant while changing its representation.
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
- 48/100