← Guides

Unix Timestamps: Seconds vs Milliseconds (and How to Convert)

What a Unix timestamp is

A Unix timestamp is the number of seconds (or milliseconds) since 00:00:00 UTC on 1 Jan 1970 — the “Unix epoch.” It’s a timezone-free way to store a point in time.

10 digits vs 13 digits

  • Seconds (e.g. 1718900000) — 10 digits. Used by most backend and systems languages.
  • Milliseconds (e.g. 1718900000000) — 13 digits. Used by JavaScript (Date.now()) and many web APIs.

How to tell them apart

Count the digits: 10 → seconds, 13 → milliseconds. If a value resolves to a year around 50000, you likely mixed the two up.

Converting

In JavaScript: Date.now() returns milliseconds; Math.floor(Date.now() / 1000) returns seconds. Divide or multiply by 1000 to switch between them.

Why the epoch starts in 1970

Unix was designed in the late 1960s on machines with limited storage, so its authors chose a single integer counting forward from a convenient recent date: 00:00:00 UTC on 1 January 1970. That instant is called the Unix epoch. The format won because it needs no time zone table, no calendar rules and no daylight saving logic — just a number that can be compared, sorted and stored in a database column.

Seconds, milliseconds, microseconds and nanoseconds

UnitDigits for a date near 2025Typical source
Seconds10POSIX time(), PHP, MySQL UNIX_TIMESTAMP(), Go Unix()
Milliseconds13JavaScript Date.now(), Java System.currentTimeMillis()
Microseconds16Many C APIs and databases with microsecond precision
Nanoseconds19Go UnixNano(), Java Instant.now() internally

The digit count is a useful clue but not a law. It works reliably for dates between roughly 1973 and 2286. A birth date in 1955 has fewer digits, and a value from a system using a different epoch can look identical to a valid one. Treat the digit count as a quick check, and confirm the unit from the source API or the column definition whenever the data crosses a system boundary.

Detecting the unit in code

A common defensive approach is a magnitude test. Values below roughly 1e11 are treated as seconds, values around 1e12 as milliseconds, and values near 1e15 as microseconds. That heuristic fails at the edges and it silently accepts nonsense, so it belongs only in import or migration code where the source is already known to be inconsistent. In application code, keep the unit in the name: createdAtMs, retryAfterSeconds, and a database column whose comment states the unit. Naming is cheaper than debugging a date that is 55 years in the future.

Conversion recipes

JavaScript     seconds = Math.floor(Date.now() / 1000)
               ms      = seconds * 1000
               Date    = new Date(ms)

Python         seconds = time.time()            # float
               ms      = int(seconds * 1000)

Go             seconds = t.Unix()
               ms      = t.UnixMilli()

Java           seconds = Instant.now().getEpochSecond()
               ms      = System.currentTimeMillis()

SQL (Postgres) SELECT to_timestamp(1718900000)
               SELECT extract(epoch FROM now())::bigint

Use integer division or an explicit floor when converting milliseconds to seconds. Rounding instead of truncating can move a value one second into the future or the past, which matters for expiry checks, rate limits and records ordered by timestamp.

The timestamp is UTC; display is local

An epoch value names a single instant and carries no time zone. Converting it to text is where time zones appear: toISOString() renders UTC, while toLocaleString() renders the viewer’s locale and zone. Two developers in different countries looking at the same value will see different clock times, and both are correct. Store and compare epoch values, and format only at render time. Saving a formatted local string such as “2026-11-03 09:00” into a column meant for timestamps is one of the most common ways a time zone bug enters a system.

Precision, rounding and clock drift

Seconds resolution is enough for logs, cache expiry and most scheduling, but not for measuring elapsed time inside a process. Wall-clock time can move backwards when the system clock is corrected by NTP or by an operator, so elapsed durations should come from a monotonic clock (performance.now() in JavaScript, System.nanoTime() in Java, time.monotonic() in Python). Converting the result to seconds is fine; relying on Date.now() for a stopwatch is not.

Leap seconds are another edge case. Unix time ignores them: the epoch counts fixed 86,400-second days, so the clock is not a perfect count of physical seconds elapsed. The difference is a few dozen seconds accumulated since 1970, which matters for astronomy and satellite work but not for application timestamps.

Before 1970 and after 2038

Older dates are represented as negative numbers, which many systems handle correctly and some older APIs reject. The more pressing boundary is the 32-bit signed integer limit: 2147483647 seconds is 03:14:07 UTC on 19 January 2038. Systems still storing epoch seconds in a signed 32-bit field will overflow on that date. Unsigned 32-bit values run out in 2106, and 64-bit values are effectively safe for any date a program will handle. Check the column type in your database, not just the type in your application code — an INTEGER column in MySQL is 32-bit even when the application uses 64-bit integers.

Negative values and the 2038 limit share the same lesson: the epoch number is only as safe as the storage chosen for it. When a date conversion looks wrong by decades, check the width and signedness of the field before you check the formula.

Mistakes worth recognising

  • Multiplying an already-millisecond value by 1000 — the result lands tens of thousands of years in the future and every date comparison breaks.
  • Storing seconds in an INT column — fine until 2038, then an overflow that is hard to spot in advance.
  • Sorting mixed units as text — a 13-digit value and a 10-digit value do not sort chronologically when compared as strings.
  • Comparing a local formatted date with a stored UTC instant, which makes jobs run hours off around a time zone boundary.

Checklist for clean timestamps

  • Name the unit in the variable, the API field and the database comment.
  • Store UTC instants; convert only for display and export.
  • Use 64-bit storage for anything that might see a date after 2038.
  • Truncate rather than round when reducing precision.
  • Keep timestamps as numbers until the final formatting step.

The same care applies to scheduled jobs, which are usually evaluated against the server’s clock: see Cron expressions explained.

Use our Unix Timestamp Converter to convert either direction and see the current timestamp live. It accepts both 10-digit and 13-digit values and shows the result in your local zone as well as in UTC.

Try it yourself: open the Timestamp Converter →