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

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

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

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

ベストアンサー1

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);

おすすめ記事