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: 2020-11-24T10:30:39+05:30

287 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements