Home > OS >  Typescript date not the same
Typescript date not the same

Time:08-05

I just want to know why these two dates in ISO format are not the same.

const d1 = '2022-08-04T08:16:32.716904'
const d2 = '2022-08-04T08:16:32.716Z'

console.log(new Date(d1).toLocaleString())
// "04/08/2022, 08:16:32"
console.log(new Date(d2).toLocaleString())
// "04/08/2022, 10:16:32"

CodePudding user response:

The timezone of of d1 is not the same of the timezone of d2. d2 is in Zulu (UTC) while d1 is given in your local timezone since the timezone info was not provided in the datestring.

Notice d2 has a Z letter after the time. d1 misses this.

You can look up more info about ISO datestrings and their timezone offset here: https://en.wikipedia.org/wiki/ISO_8601

CodePudding user response:

The Z stands for the Zero timezone, as it is offset by 0 from the Coordinated Universal Time (UTC).

Source: https://stackoverflow.com/a/29282022/11659853

This means the two Date objects have different time zones. The other Date object probably has your local time zone.

  • Related