The easiest way to get a timestamp in JavaScript

Jun 11, 2018 · by Tim Kamanin

// Get current timestamp
const nowTimestamp = Math.floor( / 1000);

// Get timestamp for a date
const dateTimestamp = Math.floor(+new Date("2017-12-31") / 1000);

To those wondering what the unary + operator does in this example: it tries to convert a value into a number. new Date("2017-12-31") returns a string by default, the unary + operator that precedes it, converts that string value to a number by calling toNumber() method on the Date object. As simple as that :)

Want to get more 🔥 tips like this one?

Subscribe to get notified about new dev tutorials