The iat
and exp
values are indeed in terms of seconds since the Unix epoch. Since the Unix epoch is UTC based, so are these values. You are converting them to Date
objects correctly.
var iat = new Date(1572468316 * 1000);
var exp = new Date(1572468916 * 1000);
console.log(iat.toISOString()); //=> "2019-10-30T20:45:16.000Z"
console.log(exp.toISOString()); //=> "2019-10-30T20:55:16.000Z"
The toISOString
function emits a string in ISO 8601 format, presented as UTC (as indicated by the trailing Z
character).
The output format you gave in your question is what would happen when calling the toString
function, or when directly logging a Date
object in an environment that chooses to log the toString
output. Since different environments can emit different formats (some in local time, some in UTC, some in ISO 8601, some in a non-standard format) - it's not recommended to directly log a Date
object.
The output from toISOString
shows that the JWT was valid for 10 minutes, from 2019-10-30 20:45:16 UTC to 2019-10-30 20:55:16 UTC. There is no other way to interpret these results.
Your local time output shows a three-hour conversion, which would correctly correspond to a UTC-3 time zone offset, again confirming they values are being parsed correctly.
If that is not what you expected, then the token wasn't issued correctly. One of two things is going on:
There could be a bug in the code that is generating the JWT, in that it is using a local time instead of UTC as a basis for creating the timestamp.
The server running the code where the JWT was generated could have its clock set incorrectly.
The second one is more plausible. For example, this can happens when a server administrator sets the the time zone to UTC but sets the clock based on the local time. Instead, ensure the clock on the server is set to synchronize its time automatically from the Internet, and the problem will likely go away.
Regarding the approach mentioned in the question comments - don't do that. Subtracting the time zone offset in that way doesn't adjust correctly for time zones, but rather it picks a different point in time. It may appear to deliver the correct results, but you will find edge cases around daylight saving time transitions in many time zones, and you will have a problem once the JWTs start being generated with the correct values.