Convert HH:MM:SS to seconds with JavaScript?


To convert the HH:MM:SS to seconds, you need to multiply the HOUR with 60X60 and Minute with 60:

Example

var time = '10:8:20';
var actualTime = time.split(':');
console.log("The time="+time);
var totalSeconds = (+actualTime[0]) * 60 * 60 + (+actualTime[1]) * 60 + (+actualTime[2]);
console.log("Total Seconds="+totalSeconds);

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

node fileName.js.

Here, my file name is demo61.js.

Output

This will produce the following output −

PS C:\Users\Amit\JavaScript-code> node demo61.js
The time=10:8:20
Total Seconds=36500

Updated on: 03-Sep-2020

758 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements