dayjs icon indicating copy to clipboard operation
dayjs copied to clipboard

Wrong offset for America/Phoenix

Open vini-brito opened this issue 2 years ago • 1 comments

Describe the bug When using America/Phoenix, it gives an offset of 4 hours from UTC/ZULU/GMT, but the real offset is 7 hours.

Expected behavior Expected offset is 7 hours, specifically, 420 minutes, but it outputs 240.

Full code to be ran in Node:

const dayjs = require('dayjs');
const customParseFormat = require('dayjs/plugin/customParseFormat')
const utc = require('dayjs/plugin/utc')
const timezone = require('dayjs/plugin/timezone')

dayjs.extend(customParseFormat)
dayjs.extend(utc)
dayjs.extend(timezone)


let date = dayjs.tz("2021/12/25", "YYYY/MM/DD", "America/Phoenix");
let date2 = dayjs.tz("2021/12/25", "YYYY/MM/DD", "zulu");

console.log(date["$d"], date["$x"]);
console.log(date2["$d"], date2["$x"]);

The output is:

2021-12-25T04:00:00.000Z { '$localOffset': 240, '$timezone': 'America/Phoenix' } 2021-12-25T00:00:00.000Z { '$timezone': 'zulu' }

Also, running date.toISOString() and date2.toISOString() it outputs the right strings:

2021-12-25T07:00:00.000Z 2021-12-25T00:00:00.000Z

Information

  • Day.js Version 1.10.7
  • Linux
  • Node v14.19.2

vini-brito avatar May 13 '22 00:05 vini-brito

Could not reproduce your error, neither on Windows nor on Linux.

My environments:

  • Day.js Version 1.12.2
  • Windows 10
  • Node v16.15.0

And

  • Day.js Version 1.12.3
  • Ubuntu 20.04.4
  • Node v14.19.2

Code:

const datePhoenix = dayjs.tz('2021/12/25', 'YYYY/MM/DD', 'America/Phoenix')
const OffsetPhoenix = datePhoenix.utcOffset()
const dateGmt = dayjs.tz('2021/12/25', 'YYYY/MM/DD', 'zulu')
const OffsetGmt = dateGmt.utcOffset()

console.log(OffsetPhoenix)  // -420
console.log(OffsetGmt)  // 0

console.log(datePhoenix.format())  // 2021-12-25T00:00:00-07:00
console.log(dateGmt.format())  // 2021-12-25T00:00:00+00:00

BePo65 avatar Jun 07 '22 09:06 BePo65

Just a more extensive explanation:

  • dayjs.tz("2021/12/25", "YYYY/MM/DD", "America/Phoenix") parses the given string for the given timezone. So date.format() will show '2021-12-25T00:00:00-07:00' - as expected.
  • looking at the internal variables is another topic; $d is a plain javescript Date object that is generated by parsing the given date string with new Date("2021/12/25").
    in your case timezone offset equals to -4h (I suppose you are living on the east coast) as can be seen by looking at $x.$localOffset.

So if you want to access your parsed dayjs object, it is certainly better to stay with the documented properties and methods.

BePo65 avatar Nov 05 '22 16:11 BePo65