Skip to content
Future Forge ToolsWeb Tools ProOpen free tools

Developer Notes

Is Unix Time in Seconds or Milliseconds? Code for Each Language

Unix time is in seconds, but JavaScript and Java use milliseconds. Copy-ready code to convert dates and timestamps in JS, Python, PHP, Java, SQL and bash.

Is Unix Time in Seconds or Milliseconds? Code for Each Language
On this page

Unix time is measured in seconds: it counts the seconds elapsed since 00:00:00 UTC on January 1, 1970. JavaScript and Java, however, use milliseconds by default. To tell them apart, count the digits. A current timestamp in seconds has 10 digits, while the same moment in milliseconds has 13.

So 1700000000 and 1700000000000 are the same instant, November 14, 2023, 22:13:20 UTC. Mixing them up is the single most common timestamp bug, and it’s easy to fix once you know which functions return which unit.

This page is a practical cheat sheet: the right call for each language, in both directions. For the background on epochs, time zones and the 2038 problem, see Unix timestamps explained.

Try it: Timestamp ConverterConvert Unix epoch timestamps (seconds or milliseconds) to human-readable dates in any time zone, and dates back to timestamps.

Open tool — free

Is Unix time in seconds or milliseconds?

The POSIX definition is seconds, and that’s what Linux, PHP, Python’s time.time(), MySQL and PostgreSQL use. Milliseconds are a convention some languages adopted for their own clocks, not a different kind of Unix time.

Language or systemDefault unitCurrent time call
Linux / bashSecondsdate +%s
JavaScriptMillisecondsDate.now()
JavaMillisecondsSystem.currentTimeMillis()
PythonSeconds (float)time.time()
PHPSecondstime()
MySQLSecondsUNIX_TIMESTAMP()
PostgreSQLSeconds (numeric)EXTRACT(EPOCH FROM now())

The digit rule holds for dates between September 2001 and November 2286. Older or far-future dates have fewer or more digits, so for historical data check the magnitude rather than counting blindly.

Convert time to a Unix timestamp (and back) by language

Each snippet shows getting the current timestamp, converting a date to a timestamp, and converting a timestamp to a date. All of them use UTC on purpose.

JavaScript

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

const ts = Date.parse('2024-03-01T12:00:00Z') / 1000; // 1709294400
const d  = new Date(1709294400 * 1000);       // seconds -> Date
d.toISOString();                              // '2024-03-01T12:00:00.000Z'

Python

import time
from datetime import datetime, timezone

now = int(time.time())
ts = int(datetime(2024, 3, 1, 12, tzinfo=timezone.utc).timestamp())
d = datetime.fromtimestamp(1709294400, tz=timezone.utc)

Watch out for naive datetimes. Calling .timestamp() on a datetime without tzinfo assumes the server’s local time zone.

PHP

$now = time();
$ts  = strtotime('2024-03-01 12:00:00 UTC');
echo gmdate('Y-m-d H:i:s', 1709294400);
$ms  = (int) floor(microtime(true) * 1000);  // milliseconds

Java

import java.time.Instant;

long ms  = System.currentTimeMillis();
long sec = Instant.now().getEpochSecond();
Instant fromSec = Instant.ofEpochSecond(1709294400L);
Instant fromMs  = Instant.ofEpochMilli(1709294400000L);

SQL: MySQL and PostgreSQL

-- MySQL (uses the session time zone for DATETIME values)
SELECT UNIX_TIMESTAMP();
SELECT UNIX_TIMESTAMP('2024-03-01 12:00:00');
SELECT FROM_UNIXTIME(1709294400);
SELECT FROM_UNIXTIME(1709294400000 / 1000);  -- from milliseconds

-- PostgreSQL
SELECT EXTRACT(EPOCH FROM now())::bigint;
SELECT EXTRACT(EPOCH FROM TIMESTAMPTZ '2024-03-01 12:00:00+00')::bigint;
SELECT to_timestamp(1709294400);

In MySQL, run SET time_zone = '+00:00'; first if you want results in UTC rather than the server’s zone.

Linux time converter: the date command

You don’t need a separate tool on Linux. GNU date converts in both directions:

date +%s                                    # now, in seconds
date +%s%3N                                 # now, in milliseconds (GNU)
date -u -d @1709294400                      # timestamp -> date
date -u -d '2024-03-01 12:00:00' +%s        # date -> timestamp

# macOS / BSD
date -u -r 1709294400
date -j -u -f '%Y-%m-%d %H:%M:%S' '2024-03-01 12:00:00' +%s

The -u flag matters. Without it, the date is read and printed in the machine’s local zone.

Common bugs: dates in 1970 or the year 50000

The wrong unit produces results so absurd they’re easy to spot once you know the pattern.

  • A date in January 1970. You passed seconds where milliseconds were expected. new Date(1700000000) lands on January 20, 1970. Multiply by 1,000.
  • A year around 55,000. You passed milliseconds where seconds were expected. Python’s fromtimestamp(1700000000000) will usually throw an error, while other tools print a far-future year. Divide by 1,000.
  • Off by a few hours. A date string was parsed without a zone, so it was read as local time. Add Z or an explicit offset.
  • Precision loss. Storing milliseconds in a 32-bit integer or a float column truncates them. Use a 64-bit integer.
  • Mixed units in one table. Name columns with the unit, such as expires_at_ms, so nobody has to guess.

Detecting the unit in code

If you accept timestamps from several sources, a magnitude check is safer than guessing. Anything above about 100 billion can’t be a plausible date in seconds, so treat it as milliseconds:

function toSeconds(ts) {
  return ts > 1e11 ? Math.floor(ts / 1000) : ts;
}

Use this as a safety net, not a design. The better fix is to document the unit in your API and stick to one. Many REST APIs and JWT claims such as exp use seconds; browser and Java code tends to produce milliseconds. When an integration breaks right after someone moves logic from the backend to the frontend, this is often why.

Checking a timestamp without writing code

When you just need to read a value from a log or API response, our free timestamp converter is quicker than opening a console. It accepts seconds or milliseconds and runs entirely in your browser.

  1. Paste the timestamp into the converter.
  2. Choose seconds or milliseconds, using the 10 versus 13 digit rule.
  3. Pick a time zone, or keep UTC to compare against server logs.
  4. Check that the year is plausible. If it’s 1970 or tens of thousands of years away, switch the unit.
  5. To go the other way, enter a date and zone and copy the resulting timestamp.

The rule to remember

Unix time is seconds. JavaScript and Java hand you milliseconds, so divide by 1,000 when you send values to Unix-style systems and multiply when you receive them. Always parse dates with an explicit zone, and if a result looks absurd, check the unit in the timestamp converter before you debug anything else.

Frequently asked questions

How do I convert a date to a Unix timestamp?

Parse the date with an explicit time zone or UTC offset, then ask for the epoch value: Math.floor(Date.parse(s) / 1000) in JavaScript, datetime.timestamp() in Python, strtotime() in PHP or date +%s in bash. Leaving out the zone is the usual source of off-by-hours errors.

How do I convert a millisecond timestamp to seconds?

Divide by 1,000 and drop the remainder, for example Math.floor(ms / 1000) in JavaScript or ms // 1000 in Python. To go the other way, multiply seconds by 1,000.

Is there a Linux command to convert Unix time?

Yes. On Linux, date -u -d @1700000000 prints the date for a timestamp and date +%s prints the current Unix time. On macOS, use date -u -r 1700000000 instead.

Why does my timestamp convert to January 1970?

You are almost certainly treating a seconds value as milliseconds. A 10-digit number read as milliseconds only covers a few weeks after the epoch, so multiply it by 1,000 first.

Free tools for this

admin

Writes practical guides for Future Forge Tools, where every tool runs privately in your browser.