Seconds, milliseconds, microseconds or nanoseconds?
A timestamp does not say what unit it is in. You can tell from the number of digits, and you need to, because a factor of 1000 turns today into 1970 or into the year 55,000.
Count the digits
| Digits | Unit | Example for the same instant |
|---|---|---|
| 10 | seconds | 1700000000 |
| 13 | milliseconds (thousandths) | 1700000000000 |
| 16 | microseconds (millionths) | 1700000000000000 |
| 19 | nanoseconds (billionths) | 1700000000000000000 |
These counts hold for every date from 2001-09-09 (timestamp 1,000,000,000) to 2286-11-20, when seconds reach 11 digits. A value with a decimal point, such as 1700000000.123, is seconds with a fraction, which is what Python's time.time() returns.
The converter on this site applies the same idea by size: below 1011 it reads seconds, below 1014 milliseconds, below 1017 microseconds, and anything larger as nanoseconds.
Where the heuristic breaks
Digit counting assumes a recent date. A millisecond timestamp for early 1970 has few digits: 86400000 is one day after the epoch in milliseconds, but read as seconds it is September 1972. If your data can include dates before 2001, or durations rather than instants, the digit count cannot decide and you must know the unit from the source.
Which unit each system uses
| Unit | Where you meet it |
|---|---|
| Seconds | Unix time(), date +%s, PHP, Ruby to_i, MySQL UNIX_TIMESTAMP(), JWT exp and iat claims, HTTP Max-Age, most REST APIs |
| Seconds as a float | Python time.time(), PHP microtime(true), PostgreSQL extract(epoch …) |
| Milliseconds | JavaScript Date.now(), Java System.currentTimeMillis(), Kafka, Elasticsearch, MongoDB dates, many mobile and analytics APIs |
| Microseconds | PostgreSQL internally, Python datetime resolution, Chrome and Google Analytics event data |
| Nanoseconds | Go UnixNano(), Java Instant, Linux clock_gettime, InfluxDB, OpenTelemetry |
Converting
Multiply or divide by 1000 per step, and round down when losing precision:
seconds = Math.floor(milliseconds / 1000)
milliseconds = seconds * 1000
milliseconds = Math.floor(microseconds / 1000)Three things go wrong in practice:
- Integer width. Milliseconds need more than 32 bits:
1700000000 * 1000overflows a 32-bit integer in C, Java and C#. Use a 64-bit type. - JavaScript precision. Numbers are exact up to 253, about 9 × 1015. Seconds, milliseconds and today's microseconds fit; nanosecond timestamps do not, and are silently rounded when parsed from JSON. Carry them as strings or
BigInt. - Rounding negative values. Truncating division rounds towards zero, so -1500 ms becomes -1 s when the correct floor is -2 s. Use a floor function for dates before 1970.
Symptoms of the wrong unit
- The date is in January 1970: seconds were passed where milliseconds were expected.
- The year is around 55,000, or the library throws "out of range": milliseconds were passed as seconds.
- A token "expires" 50,000 years from now, or expired in 1970: the
expclaim was set in milliseconds. JWT times are in seconds, which the JWT decoder shows as dates.
Questions
Is a 13-digit timestamp in milliseconds?
Almost certainly. For any date between 2001 and 2286, milliseconds have 13 digits and seconds have 10.
Why does JavaScript use milliseconds?
Its Date type was modelled on Java's java.util.Date in 1995, which counts milliseconds since the epoch.
How do I convert milliseconds to seconds?
Divide by 1000 and round down: Math.floor(ms / 1000) in JavaScript, ms // 1000 in Python, ms / 1000 with integer types in Java, C# or Go.