date-fns / tz

date-fns timezone utils
MIT License
58 stars 2 forks source link

TZDate class toISOString() method not returning correct ISO format #15

Open krema opened 2 weeks ago

krema commented 2 weeks ago

The toISOString() method of the TZDate class is not returning the correct output. According to the ISO 8601 standard, the toISOString() method should return a string in the format YYYY-MM-DDTHH:mm:ss.sssZ, where the timezone is always UTC, denoted by the suffix Z.

See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString

Current Behavior: The method currently returns:

// 2023-05-12T12:00:00.000+02:00    
new TZDate(2023, 4, 12, 12, 0, 0, 'Europe/Berlin').toISOString()

Please note that the format is wrong

Expected Behavior: The method should return (I'm located in 'Europe/Berlin') :

// 2023-05-12T10:00:00.000Z
new Date(2023, 4, 12, 12, 0, 0).toISOString()

I'm using v1.1.1

johnny-T commented 1 week ago

Hi, I am running into this issue as well in version 1.1.2. Not only the toISOString() method, but also the toJSON() method formats the time not in UTC but in local TZ.

janneh commented 1 week ago

Same here. Also expected it to follow the ISO 8601 standard and that this should hold:

test("toISOString", () => {
  const sg = new TZDate(2022, 2, 13, "Asia/Singapore")
  const ny = sg.withTimeZone("America/New_York")

  expect(
    sg.toISOString()
  ).toBe(
    ny.toISOString()
  )
})

but currently results in:

+ Expected: "2022-03-12T11:00:00.000-05:00"
- Received: "2022-03-13T00:00:00.000+08:00"