Skip to main content

Seconds vs Milliseconds Timestamps

How to recognise the unit of a timestamp, which platforms use which, and how to convert safely.

Count the digits

For dates between 2001 and 2286, the number of digits gives away the unit. Ten digits is seconds, thirteen digits is milliseconds, sixteen is microseconds and nineteen is nanoseconds. For example 1790000000 is seconds, 1790000000000 is milliseconds, 1790000000000000 is microseconds and 1790000000000000000 is nanoseconds.

The Castov converters use the same idea in their Auto setting: values with twelve or more digits are read as milliseconds, everything else as seconds. That rule can be wrong only for milliseconds before March 1973 and seconds after the year 5138.

Which platforms use which

Seconds are the classic Unix unit. You get them from the C time() function, the date +%s command, PHP's time(), Go's Unix(), MySQL's UNIX_TIMESTAMP() and PostgreSQL's EXTRACT(EPOCH FROM ...). JSON Web Token claims such as exp and iat are also defined in seconds.

Milliseconds are common where sub-second precision matters or where the language grew up around the browser: JavaScript's Date.now(), Java's System.currentTimeMillis() and .NET's ToUnixTimeMilliseconds(). Always check the documentation of the API you are calling, because the same field name can hold either unit.

The classic bugs

If you read a seconds value as milliseconds, the date lands a few weeks after 1 January 1970, because 1790000000 milliseconds is only about 20 days. If you read a milliseconds value as seconds, you get a date tens of thousands of years in the future, and many libraries report an invalid date. Either symptom tells you the unit is wrong.

Converting safely

Multiply seconds by 1,000 to get milliseconds, and divide milliseconds by 1,000 to get seconds. When you divide, decide whether to round down or keep the fraction. Rounding down with Math.floor is the usual choice, because it never moves a timestamp into the future.

const seconds = Math.floor(Date.now() / 1000);
const milliseconds = seconds * 1000;