DEV Community

Cover image for JWT exp is in seconds, not milliseconds: what I found testing three libraries
Auth Parse
Auth Parse

Posted on

JWT exp is in seconds, not milliseconds: what I found testing three libraries

You sign a token with a 60 second life, and a moment later the library says jwt expired. Or the opposite happens: the token works for days and nobody knows why it never runs out.

Both usually come from one mistake. The JWT exp claim counts seconds, and a lot of JavaScript counts milliseconds. A factor of 1000 is enough to make a token expire at once, or never.

I tested this with jsonwebtoken 9.0.3, jose 6.2.17 and PyJWT 2.15.0 (Node 22, Python 3.13). Everything below ran locally. The results are not the same across libraries, and that is the part most guides skip.

Seconds or milliseconds: count the digits

RFC 7519 defines exp, iat and nbf as a NumericDate: seconds since 1970-01-01 UTC. Date.now() returns milliseconds.

The fastest check is the length of the number:

seconds   1794863359       10 digits   2026-11-16 21:09:19 UTC
millis    1794863359316    13 digits   read as seconds: year 58846
Enter fullscreen mode Exit fullscreen mode

A 10 digit number is seconds. A 13 digit number is milliseconds.

expiresIn: '60' expires immediately

In jsonwebtoken, these two lines look almost the same:

import jwt from "jsonwebtoken";

// 60 seconds
jwt.sign({ sub: "user1" }, secret, { expiresIn: 60 });

// The string "60" means 60 milliseconds
jwt.sign({ sub: "user1" }, secret, { expiresIn: "60" });
Enter fullscreen mode Exit fullscreen mode

The first gives a 60 second token. The second gives a token that is already expired: exp equals iat, and jwt.verify throws TokenExpiredError: jwt expired right away.

The README says it: a string without units is read as milliseconds, so "120" equals "120ms". In the source, strings go through the ms package, and ms('60') is 60 milliseconds.

Here is the full list I ran:

expiresIn: 60           lifetime 60 s      verify OK
expiresIn: '60'         lifetime 0 s       TokenExpiredError: jwt expired
expiresIn: '60s'        lifetime 60 s      verify OK
expiresIn: '1h'         lifetime 3600 s    verify OK
expiresIn: '60000'      lifetime 60 s      verify OK
expiresIn: 60 * 1000    lifetime 60000 s   verify OK (16 h 40 min)
expiresIn: 90.5         rejected: must be a number of seconds or a timespan string
Enter fullscreen mode Exit fullscreen mode

Note 60 * 1000. It looks like a fix for milliseconds, but it gives a token that lives for almost 17 hours. If you use strings, always write the unit: '60s', '15m', '1h'.

exp: Date.now() never expires

Now the opposite mistake. You build the payload yourself:

// Wrong: Date.now() is in milliseconds
const token = jwt.sign({ sub: "user1", exp: Date.now() + 60 * 1000 }, secret);
jwt.verify(token, secret); // no error

// Right: seconds
const exp = Math.floor(Date.now() / 1000) + 60;
jwt.sign({ sub: "user1", exp }, secret);
Enter fullscreen mode Exit fullscreen mode

The millisecond number is read as seconds, so the token expires around the year 58000. On November 16, 2026 I got 58846. The year moves up by about one every 8.8 hours, so your number will differ.

This is worse than instant expiry. Instant expiry fails loudly and you fix it in minutes. A token that never expires passes every test and stays dangerous.

None of the three libraries stopped it. jsonwebtoken, jose and PyJWT all verified a token whose exp was a millisecond timestamp. A library cannot know you meant milliseconds.

The libraries do not agree

Same input, different results:

Input jsonwebtoken 9.0.3 jose 6.2.17 PyJWT 2.15.0
A number for the lifetime expiresIn: 60 is 60 seconds from now setExpirationTime(60) is an absolute time, so exp=60 (1970) no lifetime option
A bare string '60' read as 60 ms, exp equals iat rejected: Invalid time period format not applicable
exp from Date.now() accepted accepted accepted
iat in milliseconds accepted accepted rejected: ImmatureSignatureError

The jose row catches people who move between libraries. In jsonwebtoken a number means "seconds from now". In jose, a number passed to setExpirationTime is an absolute timestamp. So setExpirationTime(60) is January 1970, and the token is expired the moment it is signed. Use '60s' or '1h'.

The same trap on the client

Reading the token has the same problem:

// exp comes from the decoded payload, in seconds

// Wrong: every token looks expired
const expiredWrong = exp < Date.now();

// Right: the same unit on both sides
const expired = exp * 1000 < Date.now();
// or: exp < Date.now() / 1000
Enter fullscreen mode Exit fullscreen mode

exp is about 1.79 billion and Date.now() is about 1.79 trillion, so the first check calls every token expired. That can cause a refresh loop, because the new token also looks expired.

In Python the value hides until you turn it into a date:

import jwt, time, datetime as dt

ms = int(time.time() * 1000)
token = jwt.encode({"sub": "user1", "exp": ms}, secret)
jwt.decode(token, secret, algorithms=["HS256"])  # no error
dt.datetime.fromtimestamp(ms)  # ValueError: year 58846 is out of range
Enter fullscreen mode Exit fullscreen mode

A guard to add before you sign

This rejects values that look like milliseconds, values that are not whole seconds, and values that are not numbers:

function assertNumericDate(value, name = "exp") {
  if (typeof value !== "number" || !Number.isFinite(value)) {
    throw new TypeError(`${name} must be a number of seconds`);
  }
  if (value > 1e11) {
    throw new RangeError(`${name} looks like milliseconds: ${value}`);
  }
  if (!Number.isInteger(value)) {
    throw new RangeError(`${name} should be whole seconds: ${value}`);
  }
  return value;
}
Enter fullscreen mode Exit fullscreen mode

As seconds, 1e11 is the year 5138, so no real token goes past it, while any millisecond timestamp from 1973 on is larger.

The safest habit is to skip the math: expiresIn: '1h' in jsonwebtoken, setExpirationTime('1h') in jose, or Math.floor(Date.now() / 1000) + 3600 if you must write the number yourself.

Reading an exp value

If you only want to see what an exp means as a date, paste it into the JWT exp to date converter. Count the digits first: a 10 digit value gives a date you can compare with the clock.

I wrote the full version of this test, with a diagnosis table for each error message, here: JWT exp is in seconds, not milliseconds.

Have you seen a token that "never expired"? I would like to hear which library and what the exp was.

Top comments (0)