How to get the timestamp in JavaScript

The timestamp is the number of milliseconds elapsed since January 1, 1970. It is also known as the Unix time.

svg viewer

We can get this value using methods of the JavaScript date object:

  1. Use the valueOf() method (or + operator which invokes the valueOf() method):
console.log(new Date().valueOf());
   OR
console.log(+new Date());
  1. Use the now() method. This way, a Date object will not be created:
console.log(Date.now());

Example:

console.log(new Date().valueOf());
console.log(+new Date());
console.log(Date.now());
Copyright ©2024 Educative, Inc. All rights reserved