JavaScriptJavaScript

The easiest way to get a timestamp in JavaScript

Jun 11, 2018 · by Tim Kamanin

javascript
// Get current timestamp
const nowTimestamp = Math.floor(Date.now() / 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 :)

Hey, if you've found this useful, please share the post to help other folks find it:

There's even more:

Subscribe for updates

  • via Twitter: @timonweb
  • old school RSS:
  • or evergreen email ↓ ↓ ↓