Developer Documentation
JavaScript
Updated September 18, 2026

Unix Timestamp & Epoch in JavaScript

Master Unix timestamp generation, parsing, conversion, and timezone formatting in modern JavaScript and Node.js.

A Unix timestamp represents the total number of elapsed seconds (or milliseconds) since the Unix Epoch: January 1, 1970, 00:00:00 UTC. In JavaScript, understanding the distinction between millisecond timestamps and second timestamps is critical for cross-platform API interop.

Quick Start#

In JavaScript, Date.now() returns the current timestamp in milliseconds:

JavaScript
1// Current timestamp in milliseconds (13 digits)2const timestampMs = Date.now();3console.log("Milliseconds:", timestampMs);4// Output: 172670000000056// Current timestamp in seconds (10 digits, standard Unix epoch)7const timestampSec = Math.floor(Date.now() / );8console.log("Seconds:", timestampSec);9// Output: 1726700000

Converting a Timestamp to an ISO 8601 String#

To convert a numeric timestamp back into an ISO 8601 human-readable date string, pass milliseconds to the Date constructor:

JavaScript
1const epochSeconds = ;23// Remember to multiply by 1000 if your timestamp is in seconds!4const date = new Date(epochSeconds * );56console.log(date.toISOString());7// Output: "2024-09-18T22:53:20.000Z"

Parsing Dates into Unix Timestamps#

To convert human-readable date strings into timestamps:

JavaScript
1// Parse ISO date string to milliseconds2const msFromIso = Date.parse("2026-09-18T21:30:00Z");34// Or using Date object5const ms = new Date("2026-09-18T21:30:00Z").getTime();6const sec = Math.floor(ms / );78console.log("Timestamp in seconds:", sec);

Timezone Formatting with Intl.DateTimeFormat#

JavaScript Date instances are UTC internally. To render timestamps in specific timezones without third-party libraries (like Moment or date-fns), use the standard Intl.DateTimeFormat:

JavaScript
1const timestampMs = Date.now();23const formatter = new Intl.DateTimeFormat("en-US", {4  timeZone: "America/New_York",5  year: "numeric",6  month: "short",7  day: "numeric",8  hour: "2-digit",9  minute: "2-digit",10  second: "2-digit",11  timeZoneName: "short",12});1314console.log(formatter.format(new Date(timestampMs)));15// Output: "Sep 18, 2026, 05:30:00 PM EDT"

Seconds vs Milliseconds Detection#

APIs from Python, Go, and PHP typically provide 10-digit Unix timestamps (seconds), while JavaScript, Java, and MongoDB often use 13-digit millisecond timestamps:

JavaScript
1function normalizeToMilliseconds(timestamp) {2  // If timestamp is less than 1e11 (100 billion), it represents seconds3  if (timestamp < ) {4    return timestamp * ;5  }6  return timestamp;7}89const fromPython = normalizeToMilliseconds();        // 172670000000010const fromJs = normalizeToMilliseconds();          // 1726700000000

Best Practices#

  1. Explicitly document unit (seconds vs ms): Always name variables timestampSeconds or timestampMs to prevent 1000x scaling errors.
  2. Always persist in UTC: Store UTC epoch timestamps in database columns to avoid timezone ambiguity.
  3. Use BigInt for microsecond/nanosecond timestamps: When interacting with low-level systems (such as high-precision trace IDs), use process.hrtime.bigint() and JavaScript BigInt.

References#

Implement Unix Timestamp in other languages: