How can I get seconds since epoch in JavaScript?

JavascriptWeb DevelopmentObject Oriented Programming

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
raja
Published on 03-Sep-2020 11:05:58
Advertisements