How to convert date to seconds in JavaScript

How to convert date to seconds in JavaScript

Posted by Ervin Adams on July 25, 2022

How to convert date to seconds in JavaScript

How to convert date to seconds in JavaScript

Posted by Ervin Adams on July 25, 2022

Here is how you can convert date to seconds:

Step 1.Convert date string to object using date()

let d = new Date('2022-07-05 08:36:33');

Step 2. Convert date object to milliseconds by using getTime()

d.getTime()

Step 3. Divide the result by 1000 to convert milliseconds to seconds

d.getTime() / 1000

All together:

let d = new Date('2022-07-05 08:36:33');

let secs = d.getTime() / 1000;

console.log(secs)

 

This field is required
Your question have been successfully submitted and will be reviewed before published
Please login to ask a question