question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Timezone plugin unexpected daylight calculation

See original GitHub issue

Describe the bug When using the timezone plugin the date calculated by dayjs is unexpected for daylight datetime calculation, I’m comparing towards moment-timezone.

Expected behaviour

const moment = require('moment-timezone');
const dayjs = require('dayjs');
const utc = require('dayjs/plugin/utc');
const tz = require('dayjs/plugin/timezone');

dayjs.extend(utc);
dayjs.extend(tz);

const timezone = 'America/Vancouver';
const currentDate = new Date('2020-03-08T12:00-07:00');
const nowMoment = moment(currentDate);
const nowDayJs = dayjs(currentDate);

// milliseconds for currentDate: 1583694000000
console.log(nowDayJs.valueOf() === nowMoment.valueOf()); // -> true

// daylight
// https://www.timetemperature.com/canada/daylight_saving_time_canada.shtml
const plainDate = currentDate.toISOString().slice(0, 10); // 2020-08-08
const dlMoment = moment.tz(plainDate, timezone).set({
  hour: 12,
  minute: 30
});
const dlDayJs = dayjs.tz(plainDate, timezone)
  .set('hour', 12)
  .set('minute', 30);
console.log(dlDayJs.format() === dlMoment.format()); // -> false (true expected)
// 2020-03-07T12:30:00-08:00 !== 2020-03-08T12:30:00-07:00

Information

  • dayjs: 1.8.33
  • moment-timezone: 0.5.31
  • OS: Ubuntu 18.04.4 LTS
  • Nodejs 12.18.0
  • Time zone: GMT-07:00 DST (Pacific Daylight Time)

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:1
  • Comments:10

github_iconTop GitHub Comments

2reactions
nfantonecommented, Oct 4, 2020

Timezone support seems to be quite broken at the moment, unfortunately.

dayjs.tz("11-20-2020", "America/Los_Angeles").format()
// '2020-11-20T00:00:00-08:00'

dayjs.tz("11-20-2020", "America/Los_Angeles").toISOString()
// '2020-11-20T09:00:00.000Z' <--- 09:00:00 is incorrect here, should be 08:00:00

Compare to moment:

moment.tz('11-20-2020', 'MM-DD-YYYY', 'America/Los_Angeles').toISOString()
// '2020-11-20T08:00:00.000Z'
1reaction
notable-nathancommented, Sep 16, 2020

I have run in to the same issue. Here is my test case, which is a bit simpler than the one above:

Expected:

import dayjs from 'dayjs';
const utc = require('dayjs/plugin/utc');
const timezone = require('dayjs/plugin/timezone');
dayjs.extend(utc);
dayjs.extend(timezone);

function getDateInTimezone(date, TZ = 'America/New_York') {
  return dayjs.tz(date, TZ);
}

describe('dayjs.tz', function () {
  it('Handles DST', function () {
    expect(getDateInTimezone('2020-03-02').toDate()).toEqual(new Date('2020-03-02T05:00:00.000Z'));
    expect(getDateInTimezone('2020-03-09').toDate()).toEqual(new Date('2020-03-09T05:00:00.000Z'));
    // DST start
    expect(getDateInTimezone('2020-03-10').toDate()).toEqual(new Date('2020-03-10T04:00:00.000Z')); // !!! 
    expect(getDateInTimezone('2020-11-01').toDate()).toEqual(new Date('2020-11-01T04:00:00.000Z')); // !!!
    // DST end
    expect(getDateInTimezone('2020-11-02').toDate()).toEqual(new Date('2020-11-02T05:00:00.000Z'));
    expect(getDateInTimezone('2020-11-09').toDate()).toEqual(new Date('2020-11-09T05:00:00.000Z'));
  });
});

Actual:

  ● dayjs.tz › Handles DST

    expect(received).toEqual(expected) // deep equality

    Expected: 2020-03-10T04:00:00.000Z
    Received: 2020-03-09T04:00:00.000Z

And some information

dayjs: 1.8.35 OS: MacOS 10.15.5 Nodejs 14.10.1 Time zone: GMT-07:00 DST (Pacific Daylight Time)


Edit: I just noticed DST actually started in the early hours of March 8, so it appears dayjs may be incorrectly calculating when DST should start (March 9 should have already been using -4 offset, but instead dayjs.tz() didn’t start using -4 offset until March 10)

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to prevent Notes/Domino from adjusting for daylight ...
If the value is set to "No" and you experience behavior where the time zone setting reverts to an unexpected value, then check...
Read more >
How to Change Your WordPress Timezone - CoSchedule
Select the city nearest to you with the same timezone (A city is preferred if one is available, rather than choosing UTC options,...
Read more >
Time zone does not recognize daylight savings time
This time zone does not recognize daylight savings time. The user needed to reset their Enterprise application time zone to the proper regional ......
Read more >
rrule.js - Recurrence series changes time of day in time zone ...
Try out the CodeSandbox. Should get similar results as long as you are in a time zone that has Daylight Savings and the...
Read more >
Time zones - Product Documentation | ServiceNow
However, times appear to users in their local time zone, ... Time with the same zone offset and Daylight Savings Time (DST) schedule....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found