JavaScript timestamps

Date.now() returns milliseconds — the number-one interop bug. Divide by 1000 for Unix seconds.

[MOD.DEMO // TRY]

TRY IT — PASTE A STAMP

—

Now, in milliseconds

const nowMs = Date.now();

Now, in Unix seconds

const nowS = Math.floor(Date.now() / 1000);

Stamp to Date

const ts = 1712345678;
const d = new Date(ts * 1000);
console.log(d.toISOString()); // 2024-04-05T19:34:38.000Z

Date back to stamp

const d = new Date("2024-04-05T19:34:38.000Z");
const ts = Math.floor(d.getTime() / 1000);
console.log(ts); // 1712345678

Display for humans

const d = new Date("2024-04-05T19:34:38.000Z");
console.log(d.toLocaleString()); // local display
console.log(d.toISOString()); // UTC

Explicit API units: seconds or milliseconds

function toDate(value, unit) {
  if (unit !== "s" && unit !== "ms") throw new TypeError("Use s or ms");
  return new Date(unit === "s" ? value * 1000 : value);
}
console.log(toDate(1712345678, "s").toISOString()); // 2024-04-05T19:34:38.000Z
console.log(toDate(946684800000, "ms").toISOString()); // 2000-01-01T00:00:00.000Z

THE TIMEZONE PITFALL

new Date(1712345678) without ×1000 renders January 1970 — JavaScript Date expects milliseconds. toString() uses local time while toISOString() uses UTC for the same instant. Choose storage units according to your API contract; display with toLocaleString() and transmit a documented format.

[DOC.FAQ // INDEX]

ANSWERS

05 ITEMS
01 How do I get the current Unix timestamp in JavaScript?

Math.floor(Date.now()/1000) returns whole Unix seconds; Date.now() returns milliseconds. Use the unit the receiving API expects, not a language-wide assumption. Use floor rather than rounding to the nearest integer to avoid advancing to the next second.

02 Why does new Date(ts) show 1970?

If ts is in seconds, multiply by 1000 before passing it to Date. For example: const ts = 1712345678; console.log(new Date(ts * 1000).toISOString()); Do not infer units from digit count or magnitude: milliseconds for 2000 are below 1e12.

03 How do I format a timestamp as ISO 8601?

const ts = 1712345678; console.log(new Date(ts * 1000).toISOString()); prints 2024-04-05T19:34:38.000Z in UTC. For local human display use toLocaleString(). Avoid parsing display strings; prefer an explicitly documented interchange format.

04 How do I parse an ISO string back to a stamp?

const iso = "2024-04-05T19:34:38.000Z"; console.log(Math.floor(Date.parse(iso) / 1000)); This uses the JavaScript date-time string format with an explicit offset, not every ISO 8601 form. Check for NaN and validate untrusted calendar input separately; Date.parse is not a strict standards validator.

05 Seconds or milliseconds in APIs?

Document an explicit unit parameter (s or ms), never guess by magnitude. The complete toDate(value, unit) example above multiplies seconds by 1000, leaves milliseconds unchanged and rejects unknown units. It handles 946684800000 ms as 2000-01-01, not as seconds. Storage and response units depend on your API contract, not the backend language.

[NAV.SUITE]

KEEP EXPLORING TOOLKITS

3 PAGES
[DOC.LINKS]

ALSO WORTH A LOOK

Independent alternatives, listed for cross-checking. Presence here is not an endorsement, and corrections are welcome.

Sources and specifications