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

Incorrect .add(..) / .hour(..) when using timezones on DST #2184

Closed
episage opened this issue Jan 2, 2023 · 1 comment
Closed

Incorrect .add(..) / .hour(..) when using timezones on DST #2184

episage opened this issue Jan 2, 2023 · 1 comment

Comments

@episage
Copy link

episage commented Jan 2, 2023

Describe the bug
dayjs incorrectly adds hours when working with timezones with times crossing DST.

Expected behavior
On 2021-03-14 02:00:00 NY shifted clocks 1 hour forward. .hour(..) should take that into consideration. Same for .add(..)

var dayjs = require('dayjs');
var utc = require('dayjs/plugin/utc')
var timezone = require('dayjs/plugin/timezone');
dayjs.extend(utc)
dayjs.extend(timezone)

var moment = require('moment-timezone');

console.log(moment.tz("2021-03-14 03:00:00", "America/New_York").format());
// correct 2021-03-14T03:00:00-04:00
console.log(moment.tz("2021-03-14 03:00:00", "America/New_York").hour(1).format());
// correct 2021-03-14T01:00:00-05:00

console.log(dayjs.tz("2021-03-14 03:00:00", "America/New_York").format());
// correct 2021-03-14T03:00:00-04:00
console.log(dayjs.tz("2021-03-14 03:00:00", "America/New_York").hour(1).format());
// INCORRECT 2021-03-14T01:00:00-04:00, time offset is wrong

Information

  • Day.js Version: 1.11.7
  • moment Version: 2.29.4
  • moment-timezone Version: 0.5.40
  • OS: macOS 11.6
  • Browser: node v16.13.0
  • Time zone: Mon Jan 02 2023 10:42:19 GMT+0100 (Central European Standard Time)
@episage episage changed the title Wrong .add(..) / .hour(..) when using timezones on DST Incorrect .add(..) / .hour(..) when using timezones on DST Jan 2, 2023
@KronosDev-Pro
Copy link

KronosDev-Pro commented Jan 10, 2023

On dev branch, on Day.js v1.11.7 and on #2118 :

const NY = 'America/New_York'

describe('issue 2184 - add in timezones on DST', () => {
  it('Sun Mar 14 2021 03:00:00 -0400, with "set 1 hour "', () => {
    dayjs.extend(timezone)
    dayjs.extend(utc)

    const date = '2021-03-14 03:00:00'
    const goodAnwser = moment(date).tz(NY).hour(1).format()

    console.log(
      dayjs(date).tz(NY).hour(1).format(), // 2021-03-13T01:00:00-05:00
      goodAnwser // 2021-03-13T01:00:00-05:00
    )

    expect(dayjs(date).tz(NY).hour(1).format()).toBe(goodAnwser) // pass
    expect(dayjs.utc(dayjs(date).utc()).tz(NY).hour(1).format()).toBe(goodAnwser) // pass
    // expect(dayjs.tz(date, NY).hour(1).format()).toBe(goodAnwser) // is not passing
  })
})

Hoping that this will solve your problem 😉

@episage episage closed this as completed Jan 10, 2023
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

2 participants