Unix timestamps in JavaScript

JavaScript counts time in milliseconds, so nearly every bug here is a missing or extra factor of 1000. The rest come from date strings parsed as local time.

Current timestamp

console.log(Math.floor(Date.now() / 1000));   // → e.g. 1763164800     seconds
console.log(Date.now());                      // → e.g. 1763164800123  milliseconds

Date.now() returns milliseconds. Divide by 1000 and round down for the 10-digit value that most APIs, JWTs and databases expect. It works the same in browsers, Node.js, Deno and Bun.

Timestamp to date

Every example uses 1700000000, which is 14 November 2023 at 22:13:20 UTC, so you can compare the output across languages or check it in the timestamp converter.

const d = new Date(1700000000 * 1000);        // Date wants milliseconds
console.log(d.toISOString());                 // → 2023-11-14T22:13:20.000Z
console.log(d.toUTCString());                 // → Tue, 14 Nov 2023 22:13:20 GMT
console.log(d.toLocaleString('en-GB', { timeZone: 'America/New_York', dateStyle: 'medium', timeStyle: 'long' }));
// → 14 Nov 2023, 17:13:20 GMT-5

A Date is only a millisecond count; it has no zone. toISOString() always prints UTC. To show another zone, pass an IANA name as timeZone to toLocaleString or Intl.DateTimeFormat; leave it out and you get the zone of the machine running the code.

Date to timestamp

console.log(Date.parse('2023-11-14T22:13:20Z') / 1000);        // → 1700000000
console.log(Date.UTC(2023, 10, 14, 22, 13, 20) / 1000);        // → 1700000000   months count from 0
console.log(Math.floor(new Date('2023-11-14T17:13:20-05:00').getTime() / 1000));   // → 1700000000

Give the parser a full ISO 8601 string with Z or an offset. The month argument of Date.UTC and of the Date constructor starts at 0, so November is 10.

Pitfalls

console.log(new Date(1700000000).toISOString());               // → 1970-01-20T16:13:20.000Z   forgot the * 1000
console.log(new Date('2023-11-14').toISOString());             // → 2023-11-14T00:00:00.000Z   date only: read as UTC
console.log(new Date('2023-11-14T00:00:00').toISOString());    // → 2023-11-14T05:00:00.000Z   no zone: read as local time (here New York)
console.log(new Date(2023, 10, 14).getTime() === Date.UTC(2023, 10, 14));   // → false   the first is local midnight
  • Seconds passed as milliseconds give a date in January 1970. If a date looks like 1970, multiply by 1000.
  • Date-only strings are UTC, date-time strings without a zone are local. That is what the language specification says, and it is why the same code gives different timestamps on a laptop and on a server.
  • Formats other than ISO 8601 (11/14/2023, 14 Nov 2023) are parsed however the engine likes. Do not rely on them.
  • A Date holds whole milliseconds between about 271,821 BC and AD 275,760. For microseconds or nanoseconds, keep the value as a BigInt or a string.

Questions

How do I get a Unix timestamp in seconds in JavaScript?

Math.floor(Date.now() / 1000). Date.now() is in milliseconds, so divide by 1000 and round down.

Why does new Date(timestamp) show 1970?

The timestamp is in seconds and Date expects milliseconds. Use new Date(timestamp * 1000).

Is Date.now() affected by the time zone?

No. It is the number of milliseconds since 1970-01-01T00:00:00Z and is the same on every machine with a correct clock. Only formatting and parsing involve a time zone.