DevRivet

Blog ·

Unix Timestamp Seconds vs Milliseconds: How to Tell and Convert

Seconds timestamps have 10 digits, milliseconds have 13. Learn how to tell them apart and convert either to a date in JavaScript, Python and the shell.

Unix Timestamp Converter

Convert between Unix timestamps and human-readable dates.

Try it free →

A Unix timestamp in seconds has 10 digits for dates between 2001 and 2286, such as 1700000000. The same instant in milliseconds has 13 digits, such as 1700000000000. Count the digits first. If you have 10 digits, treat the value as seconds. If you have 13, treat it as milliseconds. You can check any value with the Unix Timestamp Converter, which accepts either unit.

Why there are two units

The Unix epoch is 1970-01-01T00:00:00Z. Unix time counts the seconds since that moment, and it ignores leap seconds. Many languages and APIs need sub-second precision, so JavaScript, Java and many logging and analytics systems count milliseconds instead. Some databases and APIs use microseconds (16 digits) or nanoseconds (19 digits), but seconds and milliseconds cover most everyday cases.

Both values represent the same kind of thing: a count from the same epoch. The only difference is the scale, a factor of 1000. Most bugs happen when a value in one unit is read as the other.

How to tell which unit a value uses

Use the number of digits as a quick heuristic. For values in a normal modern range, this works reliably.

  • 10 digits (for example 1700000000): seconds. Covers roughly 2001-09-09 through 2286-11-20.
  • 13 digits (for example 1700000000000): milliseconds. Covers roughly 2001 through 2286 as well.
  • 16 digits: microseconds. 19 digits: nanoseconds.
  • Values with fewer digits, such as 0 or 86400, are seconds near the epoch. They are rare in practice and easy to misread.

The digit count does not tell you what a value is for. Always check the source. An API field called expires_at, a JWT exp claim, and a database column named created_ms may each use a different unit. JWT numeric dates such as exp, iat and nbf are always seconds per RFC 7519, and you can see them decoded with the JWT Decoder. For more on that claim, see JWT Expiration Time: How to Check the exp Claim.

Convert seconds to a date

In JavaScript, the Date constructor takes milliseconds. To convert seconds, multiply by 1000 first. Forgetting this step is the most common mistake.

const seconds = 1700000000;
const date = new Date(seconds * 1000);
console.log(date.toISOString());

Output: 2023-11-14T22:13:20.000Z

In Python, datetime.fromtimestamp takes seconds. Pass tz=timezone.utc so the result is explicit about the zone.

from datetime import datetime, timezone

seconds = 1700000000
dt = datetime.fromtimestamp(seconds, tz=timezone.utc)
print(dt.isoformat())

Output: 2023-11-14T22:13:20+00:00

On Linux with GNU date, use -d with an @ prefix. On macOS, use -r instead. Both flags below are standard for their platforms.

# GNU date (Linux)
date -u -d @1700000000

# BSD date (macOS)
date -u -r 1700000000

Convert milliseconds to a date

In JavaScript, pass milliseconds straight to the Date constructor. No conversion is needed.

const ms = 1700000000000;
const date = new Date(ms);
console.log(date.toISOString());

Output: 2023-11-14T22:13:20.000Z

In Python, divide by 1000 before calling fromtimestamp. The float result keeps the sub-second part, which is useful if you need it. Use integer division if you only want whole seconds.

from datetime import datetime, timezone

ms = 1700000000000
dt = datetime.fromtimestamp(ms / 1000, tz=timezone.utc)
print(dt.isoformat())

Output: 2023-11-14T22:13:20+00:00

Convert a date back to a timestamp

To get the current time, use the function that matches the unit you need. Date.now() returns milliseconds in JavaScript. Python's time.time() returns seconds as a float.

const ms = Date.now();
const seconds = Math.floor(Date.now() / 1000);
console.log(ms, seconds);
import time

seconds = int(time.time())
milliseconds = int(time.time() * 1000)
print(seconds, milliseconds)

To convert a specific date, parse it with a timezone. A date-time string without an offset is interpreted in local time in JavaScript, which can shift the result. Include a Z or an explicit offset to avoid that.

const s = Math.floor(new Date('2023-11-14T22:13:20Z').getTime() / 1000);
console.log(s);

Output: 1700000000

Converting between the two units

  • Seconds to milliseconds: multiply by 1000. Example: 1700000000 becomes 1700000000000.
  • Milliseconds to seconds: divide by 1000 and drop the fraction. In JavaScript, use Math.floor(ms / 1000). In Python, use ms // 1000.
  • Keep the unit in the variable name, such as created_at_s or created_at_ms, so the conversion is visible in the code.

Common pitfalls

  • Off by a factor of 1000: a seconds value passed to new Date() lands in January 1970. For example, 1700000000 becomes 1970-01-20. A milliseconds value treated as seconds lands around the year 33,000 or later, because 13-digit values are 1e12 seconds and up. If a date looks wildly wrong, check the unit first.
  • Local time vs UTC: Unix timestamps are always UTC. Showing them in local time is a display choice. Log and store in UTC, and convert only when you show a value to a user.
  • Strings instead of numbers: values that come from query strings or CSV files are strings. Convert them with Number() or int() before doing arithmetic. In JavaScript, '1700000000' * 1000 works by coercion, but '1700000000' + 1000 concatenates to '17000000001000'.
  • Precision loss: JavaScript numbers are exact for integers up to 2^53. Millisecond timestamps fit easily, but nanosecond timestamps do not. Use BigInt or a string-based library for values with 19 digits.
  • Negative values: timestamps before 1970 are negative, and they convert correctly in most languages. Some older tools reject them.
  • Mixed units in one system: a service may store seconds in one table and milliseconds in another. Normalize at the boundary, and name fields with a suffix such as _s or _ms.

Quick check

Paste a value into the Unix Timestamp Converter to see the date in both interpretations. If the seconds reading gives a sensible date and the milliseconds reading does not, the value is in seconds. Avoid pasting values that identify a user or session, even in a debugging tool.

FAQ

Why does my timestamp show a date in 1970?

The most likely cause is that a seconds value was passed to a function that expects milliseconds, such as JavaScript's new Date(). Multiply the value by 1000 before passing it in.

Is there a way to tell seconds from milliseconds without counting digits?

For values in a normal modern range, the digit count is the practical test. Otherwise, check the API documentation or the field name. The value itself cannot tell you its unit with certainty.

Do Unix timestamps include time zones?

No. A Unix timestamp is a single count from the epoch in UTC. Time zones appear only when you format the value as a human-readable date.

Why does Date.now() return milliseconds?

JavaScript's Date object is built around milliseconds since the epoch. Date.now() returns that value directly. Divide by 1000 and floor the result if you need seconds.

Unix Timestamp Converter

Convert between Unix timestamps and human-readable dates.

Try it free →