Search code examples
javascriptdatetimezoneiso8601

What does the values before "Z" indicate in a ISO8601 date string?


I'm trying to understand ISO8601 time format

What does this value mean?

2019-11-14T00:55:31.820Z

I understand this as such

2019-11-14 // November 14th, 2019 
T00:55:31  // 12:55 AM  in GMT time (London)
.820       // why is this needed or specified?
Z          // The "Z" indicates to store this as GMT time (London)

I don't understand why the .820 is needed here. Reading into it, this refers to timezone 820, which is California.

If I were to go to the Javascript console and write this with and without that 820 value, I would get the same results, based on my locale (EST time zone)

new Date('2019-11-14T00:55:31.820Z') // Wed Nov 13 2019 19:55:31 GMT-0500 (Eastern Standard Time)
new Date('2019-11-14T00:55:31Z')     // Wed Nov 13 2019 19:55:31 GMT-0500 (Eastern Standard Time)

What does the prefix value before the Z actually do?


Solution

  • The "Z" indicates to store this as GMT time (London)

    No, it indicates that the time value presented is in UTC, not that you should store it in GMT/UTC. It tells you what time zone to interpret the information provided in.

    I don't understand why the .820 is needed here. Reading into it, this refers to timezone 820, which is California.

    That's milliseconds, not a time zone. It's 820ms into 00:55:31 (e.g., it's 180ms away from being 00:55:32).

    If I were to go to the Javascript console and write this with and without that 820 value, I would get the same results, based on my locale (EST time zone)

    Only because the output of whatever console you're quoting doesn't include milliseconds. If you check the actual dates, you'll see that they're 820ms apart:

    const dt1 = new Date('2019-11-14T00:55:31.820Z');
    const dt2 = new Date('2019-11-14T00:55:31Z');
    
    console.log(dt1.valueOf() - dt2.valueOf()); // 820

    (The underlying value of a Date instance is the number of milliseconds it represents since Jan 1st 1970 at midnight UTC. So if we subtract d2 [which doesn't have the .820] from d1 [which does], we see they're 820ms apart.)