Every date in JS can be represented as a number of milliseconds since Jan 1, 1970. Here’s how you can get the timestamp value from any JavaScript date.

You can use the static function Date.now() to get the number of milliseconds elapsed since January 1, 1970 00:00:00 UTC.

That’s a quick win if you need the current timestamp.

const timestamp = Date.now()
console.log(timestamp); // 1640379803553

If you already have the date object, for example you got it as an argument, you can use 2 tricks.

  • you can convert the JS date to timestamp with the + operator
  • or you can use the method getTime to get a timestamp from the JS date object

Here’s a code example that shows demonstrates both approaches.

const date = new Date();

function printTimestamp(date) {
  console.log(date instanceof Date); // true

  console.log(date); // 2021-12-24T21:07:11.140Z
  console.log(+date); // 1640380031140
  console.log(date.getTime()); // 1640380031140