How to get a timestamp in JavaScript?


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);
console.log(unix);

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

const date = new Date();
const milliseconds = date.getTime();
console.log(milliseconds);

Updated on: 24-Nov-2020

226 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements