Get current date/time in seconds - JavaScript?



At first, get the current date −

var currentDateTime = new Date();

To get the current date/time in seconds, use getTime()/1000.

Example

Following is the code −

var currentDateTime = new Date();
console.log("The current date time is as follows:");
console.log(currentDateTime);
var resultInSeconds=currentDateTime.getTime() / 1000;
console.log("The current date time in seconds is as follows:")
console.log(resultInSeconds);

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

node fileName.js.

Here, my file name is demo285.js.

Output

This will produce the following output on console −

The current date time is as follows:
2020-08-28T16:13:35.987Z
The current date time in seconds is as follows:
1598631215.987

Advertisements