How to get a timestamp in JavaScript?

JavascriptWeb DevelopmentFront End TechnologyObject Oriented Programming

We are required to depict the ways in which we can access the current timestamp in JavaScript in −

  • ---seconds

  • ---milliseconds

JavaScript works with the number of milliseconds since the epoch whereas most other languages work with the seconds.

This will give you a Unix timestamp (in seconds) −

const date = new Date();
const unix = Math.round(+date / 1000);

This will give you the milliseconds since the epoch (not Unix timestamp) −

const date = new Date();
const milliseconds = date.getTime();
Published on 24-Nov-2020 10:30:23