[JavaScript] Get a Unix timestamp in seconds

Created on November 30, 2026

The getTime() method on a Date object returns milliseconds since the Unix epoch. To get whole seconds, divide by 1,000 and round down with Math.floor().

const now = new Date();
const unixTimestamp = Math.floor(now.getTime() / 1000);
console.log(unixTimestamp);

The output depends on when you run the code. For example, at 2021-07-23T14:06:19Z it prints:

1627049179
JavaScript