I want to calculate the difference between two times.
The format of the time is Intime="00:05:53"
and Outtime="00:17:31"
I've tried the code below, but I am getting a result like totalhours="00:-12:22".
instead of totalhours="00:11:38"
. What am I doing wrong? How can I go about fixing this?
var inhh=0;
var inmm=0;
var inss=0;
var outhh=0;
var outmm=0;
var outss=0;
var inTime = document.getElementById("intime").value;
var outTime = document.getElementById("outtime").value;
var intimeAarry=inTime.split(":");
inhh= parseInt(intimeAarry[0]);
inmm=parseInt(intimeAarry[1]);
inss=parseInt(intimeAarry[2]);
if (inss > 60) {
inmm = inmm + 1;
inss = inss % 60;
}
if (inmm > 60) {
inhh = inhh + 1;
inmm = inmm % 60;
}
var hh;
var mm;
var ss;
var outtimeArray=outTime.split(":");
outhh= parseInt(outtimeArray[0]);
outmm=parseInt(outtimeArray[1]);
outss=parseInt(outtimeArray[2]);
if (outss > 60) {
outmm = outmm + 1;
outss = outss % 60;
}
if (outmm > 60) {
outhh = outhh + 1;
outmm = outmm % 60;
}
hh=outhh-inhh;
mm=outmm-inmm;
ss=outss-inss;
var hhh = hh == 0 ? "0" + hh : hh;
var mmm = mm == 0 ? "0" + mm : mm;
var sss = ss == 0 ? "0" + ss : ss;
var totalhours = hhh + ":" + mmm + ":" + sss;