DEV Community

Dan Newton
Dan Newton

Posted on

Unix timestamps: seconds vs milliseconds in JavaScript

A Unix timestamp can be in seconds or in milliseconds. APIs mix them. A bug shows up when you pass the wrong one: the date lands in January 1970, or in the year 50000.

The two units

Math.floor(Date.now() / 1000)  // 1765000000   seconds (10 digits)
Date.now()                     // 1765000000000 milliseconds (13 digits)
Enter fullscreen mode Exit fullscreen mode

JavaScript Date wants milliseconds. Most Unix tools, JWT claims like exp and iat, and many REST APIs use seconds.

new Date(1765000000)      // 1970-01-21  (wrong: seconds read as ms)
new Date(1765000000 * 1000) // 2025-12-06 (right)
Enter fullscreen mode Exit fullscreen mode

If you see a date in January 1970, you passed seconds where milliseconds were expected.

Guess the unit from the size

You can guess by magnitude. Seconds since 1970 stay at 10 digits until the year 2286. Milliseconds have been 13 digits since 2001. So a value above 9,999,999,999 can only be milliseconds, for any date in the normal range.

function toMs(value) {
  return value > 9999999999 ? value : value * 1000;
}

toMs(1765000000);     // 1765000000000
toMs(1765000000000);  // 1765000000000
Enter fullscreen mode Exit fullscreen mode

This is the same check I use in the timestamp converter on DevToolss. You paste a number, it picks the unit by size and shows the date.

The guess fails for dates before April 1970 in milliseconds, or after 2286 in seconds. Real data rarely hits either. For safety, if the API docs state the unit, trust the docs over the guess.

Show local time and UTC

A timestamp has no time zone. Only the display does. Always show both when you debug.

const d = new Date(toMs(1765000000));
d.toISOString();   // "2025-12-06T05:46:40.000Z"  (UTC)
d.toString();      // in the machine's local zone
Enter fullscreen mode Exit fullscreen mode

When a ticket says "the job ran at the wrong time", the first question is which zone was used to read the number.

Relative time helps

For logs and token expiry, "3 hours ago" or "in 2 days" is faster to read than a date. Take the difference from Date.now(), divide down to seconds, minutes, hours and days, and pick the largest unit that fits. For a future time, say "in" instead of "ago".

Checking a JWT expiry

The exp claim is in seconds. To check whether a token has expired:

const expired = payload.exp * 1000 < Date.now();
Enter fullscreen mode Exit fullscreen mode

Forgetting the * 1000 means the token looks expired in 1970 terms, or never expires if you flip the units. It is a common mistake.

Summary

  • Seconds are 10 digits, milliseconds are 13.
  • Date takes milliseconds.
  • Guess by size, but prefer the documented unit.
  • Print UTC and local time side by side when you debug.

The timestamp converter runs in the browser. It needs no sign-up.

Top comments (0)