JavaScript provides several ways to get the current timestamp, which is the number of milliseconds that have passed since January 1, 1970, 00:00:00 UTC. Here are a few examples of how to get the timestamp in JavaScript:
Using the Date.now() method
const timestamp = Date.now();
console.log(timestamp); // 1613231960828 (example output)
Using the Date() constructor
const timestamp = new Date().getTime();
console.log(timestamp); // 1613231960828 (example output)
Using the performance.now() method
const timestamp = performance.now();
console.log(timestamp); // 1613231960828 (example output)
The first two methods use the built-in Date object to get the current timestamp. The Date.now() method returns the current timestamp, while the Date() constructor returns a new Date object, and the getTime() method returns the timestamp of the object.
The performance.now() method returns the timestamp with a higher precision than Date.now() and new Date().getTime() and it is more accurate on the browser.
You can also get the timestamp in a specific format using the toString() method or a library like Moment.js
Note that the timestamp is the number of milliseconds that have passed since January 1, 1970, 00:00:00 UTC. To convert it to a human-readable format, you can use the toString() method, or a library like Moment.js
const date = new Date(timestamp);
console.log(date.toString()); // "Mon May 24 2021 11:20:05 GMT-0700 (Pacific Daylight Time)" (example output)
