Timestamps
JavaScript uses millisecond timestamps internally. Unix/POSIX uses seconds. Be careful when converting between them.
JavaScript uses millisecond timestamps internally. Unix/POSIX uses seconds. Be careful when converting between them.
// JavaScript millisecond timestamp
const ms = Date.now(); // e.g. 1718000000000
const ms2 = new Date().getTime();
// Convert to Unix (seconds)
const unix = Math.floor(Date.now() / 1000);
// Convert Unix timestamp to Date
const fromUnix = new Date(unix * 1000);
// ISO string to timestamp and back
const iso = "2024-06-15T10:30:00Z";
const ts = new Date(iso).getTime();
const back = new Date(ts).toISOString();
Always be explicit about whether a timestamp is in milliseconds (JS) or seconds (Unix) — mixing them causes bugs.
More in JavaScript