Sri Reddy Sri Reddy - 7 months ago 10
Javascript Question

Convert HH:MM:SS string to seconds only in javascript

I am having similar requirement as this: Convert HH:MM:SS to seconds only?

but in javascript. I have seen many examples of converting seconds into different formats but not HH:MM:SS into seconds. Any help would be appreciated.

Answer

Try this:

var hms = '02:04:33';   // your input string
var a = hms.split(':'); // split it at the colons

// minutes are worth 60 seconds. Hours are worth 60 minutes.
var seconds = (+a[0]) * 60 * 60 + (+a[1]) * 60 + (+a[2]); 

console.log(seconds);