JavaScript Timestamp: Get and Convert Epoch Time

TL;DR: get, convert and print a timestamp
const ms = Date.now();                   // now, in milliseconds
const s = Math.floor(ms / 1000);         // now, in seconds
const date = new Date(s * 1000);         // seconds → Date
console.log(date.toISOString());         // "2025-01-01T00:00:00.000Z"

Tested on: Node.js 24 and current browsers · last tested

A JavaScript timestamp is the number of milliseconds since 00:00:00 UTC on 1 January 1970. Most other systems count seconds, and that factor of 1,000 causes most JavaScript date bugs. This guide covers both directions, formatting and Node.js timing. Find other languages in the Unix timestamp guides, or use the Unix epoch converter to check a value.

Get the Current Timestamp With Date.now()

Date.now() returns the current time as an integer number of milliseconds. It needs no object and no imports, and it works the same in browsers, Node.js, Deno and Bun.

console.log(Date.now());                    // milliseconds
console.log(Math.floor(Date.now() / 1000)); // seconds
Example output at 2025-01-01T00:00:00Z
1735689600000
1735689600

Use Math.floor, not Math.round, when converting to seconds. Rounding can push a value half a second into the future, which breaks “not before” checks in tokens and signed URLs. You can compare against the live current epoch time clock.

Browser clocks can be wrong

In a browser, Date.now() reads the visitor's device clock, and nothing stops that clock from being minutes or even years off. That matters whenever time is used for a decision: token expiry, rate limits, “posted 5 minutes ago” labels or anything security-related. Treat client timestamps as a hint. Let the server stamp the authoritative time when a record is created, and send the server's timestamp back to the client when it needs to display or compare times. If you must correct for a skewed client clock, compute the offset once from a server response and add it to later readings.

Convert a JavaScript Date to a Unix Timestamp

For JavaScript date to timestamp conversion, call getTime() on a Date. To parse a string and get the number in one step, use Date.parse(). For known calendar values in UTC, Date.UTC() skips the string entirely.

const d = new Date("2025-01-01T00:00:00Z");
console.log(d.getTime());                  // ms
console.log(Math.floor(d.getTime() / 1000)); // s
console.log(Date.parse("2025-01-01T00:00:00Z") / 1000);
console.log(Date.UTC(2025, 0, 1) / 1000);  // month 0 = January
Output
1735689600000
1735689600
1735689600
1735689600

Pitfall: a date-only ISO string is read as UTC, but a date-time string without an offset is read as local time. On a machine set to New York:

new Date("2025-01-01").toISOString();        // date only → UTC
new Date("2025-01-01T00:00").toISOString();  // no offset → local
Output with TZ=America/New_York
2025-01-01T00:00:00.000Z
2025-01-01T05:00:00.000Z

Going the other way, JSON.stringify() turns a Date into an ISO 8601 string automatically, because Date has a built-in toJSON() method: {"at":"2025-01-01T00:00:00.000Z"}. If your API expects a number, convert explicitly with getTime() before serializing.

Always include Z or an offset such as +05:00 in strings you parse. The milliseconds to date converter shows the same rules interactively.

Convert a Unix Timestamp to a Date in JavaScript

For JavaScript unix timestamp to date conversion, multiply seconds by 1,000 and pass the result to new Date(). Then pick an output: toISOString() for machines, toUTCString() for HTTP headers.

const date = new Date(1735689600 * 1000);
console.log(date.toISOString());
console.log(date.toUTCString());
Output
2025-01-01T00:00:00.000Z
Wed, 01 Jan 2025 00:00:00 GMT

An invalid input does not throw when you create the Date. It quietly becomes Invalid Date, and the error only appears later:

const bad = new Date("not a date");
console.log(String(bad));
bad.toISOString();
Output
Invalid Date
RangeError: Invalid time value

Guard with Number.isNaN(date.getTime()) before formatting user input.

UTC getters vs local getters

A Date holds one instant, but its plain getters answer in the machine's local zone. The getUTC* family answers in UTC. Mixing them is a classic off-by-one-day bug around midnight:

const d = new Date(1735689600000);
console.log(d.getUTCFullYear(), d.getFullYear());
console.log(d.getUTCHours(), d.getHours());
Output with TZ=America/New_York
2025 2024
0 19

When building dates for storage or APIs, use the UTC getters, or skip them and send toISOString().

Seconds vs Milliseconds in JavaScript

Epoch time in JavaScript is milliseconds, while Unix tools, Python, PHP and most databases use seconds. Passing a seconds value to new Date() gives a date in January 1970:

new Date(1735689600).toISOString();        // seconds passed as ms
new Date(1735689600 * 1000).toISOString(); // correct
Output
1970-01-21T02:08:09.600Z
2025-01-01T00:00:00.000Z

That mismatch is the single most common epoch time JavaScript bug. A safe rule: convert at the boundary. Values arriving from an API in seconds get * 1000 as soon as they are parsed, and values you send to such an API get Math.floor(ms / 1000) right before sending. If an API mixes both, check the digit count: 10 digits is seconds, 13 is milliseconds.

When you cannot control the input, a small helper can guess the unit. Any value below 100 billion is treated as seconds: as seconds that reaches the year 5138, while as milliseconds it would be stuck in 1973. Use it only for loose input such as user pastes, never as a substitute for knowing your data:

function toDate(value) {
  const n = Number(value);
  return new Date(Math.abs(n) < 1e11 ? n * 1000 : n);
}
console.log(toDate(1735689600).toISOString());
console.log(toDate(1735689600000).toISOString());
Output
2025-01-01T00:00:00.000Z
2025-01-01T00:00:00.000Z

Nanoseconds need BigInt

A JavaScript number holds integers exactly only up to 253 − 1, which is 9007199254740991. A 19-digit nanosecond timestamp is larger, so its last digits are lost:

console.log(Number(1735689600123456789n));

const ns = 1735689600123456789n;
const date = new Date(Number(ns / 1_000_000n)); // keep ms
console.log(date.toISOString(), ns % 1_000_000n); // remainder in ns
Output
1735689600123456800
2025-01-01T00:00:00.123Z 456789n

For the full comparison of units and the bugs each causes, read seconds vs milliseconds timestamps.

Format Dates With Intl.DateTimeFormat

Intl.DateTimeFormat formats a Date for any locale and IANA time zone without a library. The timeZone option is what most people miss: without it, output follows the user's system zone.

const date = new Date(1735689600 * 1000);

const us = new Intl.DateTimeFormat("en-US", {
  dateStyle: "full", timeStyle: "long", timeZone: "America/New_York",
});
console.log(us.format(date));

console.log(new Intl.DateTimeFormat("en-GB", {
  dateStyle: "medium", timeStyle: "short", timeZone: "Europe/London",
}).format(date));
Output
Tuesday, December 31, 2024 at 7:00:00 PM EST
1 Jan 2025, 00:00

For “3 hours ago” style text, Intl.RelativeTimeFormat does the wording; you supply the number and unit:

const rtf = new Intl.RelativeTimeFormat("en", { numeric: "auto" });
console.log(rtf.format(-3, "hour"));
console.log(rtf.format(1, "day"));
Output
3 hours ago
tomorrow

Create a formatter once and reuse it; building a new one for each row of a long table is slow. Pass undefined as the locale to follow the visitor's own language settings. Discord bots use the same numbers to build Discord timestamps, which let Discord do the formatting for each reader.

High-Resolution Time in Node.js

A Node.js timestamp from Date.now() is wall-clock time. It can jump when the system clock syncs, so it is the wrong tool for measuring how long something took. Node has two monotonic clocks for that:

const t0 = process.hrtime.bigint();   // nanoseconds, BigInt
// ... work ...
const t1 = process.hrtime.bigint();
console.log(`took ${(t1 - t0) / 1_000_000n} ms`);

const p0 = performance.now();         // milliseconds, float
// ... work ...
console.log(`took ${(performance.now() - p0).toFixed(3)} ms`);

Neither value is a Unix timestamp; both count from an arbitrary starting point. Use them only for differences between two readings in the same process. When you need a precise wall-clock reading in milliseconds with a fraction, performance.timeOrigin + performance.now() gives one, and it works in browsers too.

Back-end services often share timestamps with Python workers. The Python timestamp guide shows the matching conversions on that side.

JavaScript Timestamp FAQ

How do I get the current timestamp in JavaScript?

Date.now() returns the current Unix time in milliseconds, a 13-digit number today. For seconds, use Math.floor(Date.now() / 1000). Both work in every browser and in Node.js without imports. If you already have a Date object, call its getTime() method, which returns the same millisecond value for that date.

Why is my JavaScript date in 1970?

You passed seconds to a function that expects milliseconds. new Date(1735689600) treats the number as milliseconds, which is about 20 days after the epoch, so it shows January 1970. Multiply by 1,000 first: new Date(1735689600 * 1000). The reverse mistake, sending milliseconds to an API that wants seconds, pushes dates tens of thousands of years ahead.

Does Date.now() depend on the user's time zone?

No. Date.now() counts milliseconds since 00:00:00 UTC on 1 January 1970, so it gives the same number everywhere at the same moment. Time zones only affect how a Date is displayed, for example with toString() or Intl.DateTimeFormat. Use toISOString() when you want a UTC string that never changes with the user's settings.

How do I get a Unix timestamp in Node.js?

Use the same Date.now() as in the browser; Node.js supports it fully. For seconds, divide by 1,000 and round down. For measuring durations rather than reading the wall clock, use performance.now() or process.hrtime.bigint(), which are monotonic and do not jump when the system clock is adjusted.