JSFiddle - React, Tailwind, and code Playground
JavaScript
function toSeconds(time_str) {
// Extract hours, minutes and seconds
var parts = time_str.split(':');
// compute and return total seconds
return parts[0] * 3600 + // an hour has 3600 seconds
parts[1] * 60 + // a minute has 60 seconds
+
parts[2]; // seconds
}
var a = "10:22:57"
var b = "10:30:00"
var difference = Math.abs(toSeconds(a) - toSeconds(b));
// format time differnece
var result = [
Math.floor(difference / 3600), // an hour has 3600 seconds
Math.floor((difference % 3600) / 60), // a minute has 60 seconds
difference % 60
];
// 0 padding and concatation
result = result.map(function(v) {
return v < 10 ? '0' + v : v;
}).join(':');
alert(result);