How can I get seconds since epoch in JavaScript?


To get seconds since epoch, use the below syntax −

var anyVariableName= new Date();
Math.round(yourDateVariableName.getTime() / 1000);

At first, get the current date −

var currentDate= new Date();

Now, get seconds since epoch −

Example

var currentDate= new Date();
var epochSeconds = Math.round(currentDate.getTime() / 1000);
console.log(epochSeconds);

To run the above program, you need to use the following command −

node fileName.js.

Here, my file name is demo67.js.

Output

This will produce the following output −

PS C:\Users\Amit\JavaScript-code> node demo67.js
1594821507

Updated on: 03-Sep-2020

279 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements