-2

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;
Casey Falk
  • 2,617
  • 1
  • 18
  • 29
user2996174
  • 1,391
  • 4
  • 13
  • 20
  • 1
    You tagged the question with jquery however the code you provided has no jquery. Are you looking for a solution with only pure js or can jquery be there too? Although it doesn't look like jquery is needed at all – Huangism Jul 17 '14 at 12:56
  • 1
    This last line is pure "gimme teh codez" and will get you downvotes, I'd remove it – William Barbosa Jul 17 '14 at 12:57
  • http://jsfiddle.net/VDnND/171/ – Umesh Sehta Jul 17 '14 at 12:58
  • 1
    If you search for this, you will get a lot of different solutions... – j809 Jul 17 '14 at 12:59
  • He has shown some effort, so why not guide him? Try changing those time into seconds, calculate difference in seconds and then change back to hours. – Nebril Jul 17 '14 at 13:02

4 Answers4

2

Check out http://momentjs.com/ for all your Date and Time needs.

<script src="http://momentjs.com/downloads/moment.min.js"></script>
<script>
  (function(){

    var Intime = moment("00:05:53", "HH:mm:ss");
    var Outtime = moment("00:17:31", "HH:mm:ss");

    console.log( Outtime.diff( Intime, "minutes") );        

  })()
</script>
williamukoh
  • 566
  • 6
  • 9
1

try this would be helpful

var diff = ( new Date("1970-1-1 " + Outtime) - new Date("1970-1-1 " + Intime) ) / 1000 / 60 / 60; 
NandaIN
  • 25
  • 10
0

The reason your code fails is that you can't simply subtract the parts from each other. A simple strategy is to convert them to a common unit, do the subtraction, then convert back to a suitable format.

Your code looks overly complex, consider the following. It gets the difference between two times by firstly converting them to seconds, gets the difference, then converts back to a time in hh:mm:ss format:

// Return seconds given a time in h:m:s format
function timeToSecs(t) {
  var b = t.split(':');
  return b[0]*3.6e3 + b[1]*60 + +b[2];
}

// Return hh:mm:ss given seconds
function secsToTime(s) {
  function z(n){return (n<10? '0' : '') + n}
  var sign = s < 0? '-' : '';
  s = Math.abs(s);
  return sign + z(s/3.6e3|0) + ':' + z(s%3.6e3/60|0) + ':' + z(s%60);
}

// Return the difference between two times in h:m:s format
function (t0, t1) { 
  return secsToTime(timeToSecs(t1) - timeToSecs(t0));
}

console.log(getTimeDiff('00:05:53', '00:17:31')); //  00:11:38
console.log(getTimeDiff('00:17:31', '00:05:53')); // -00:11:38
console.log(getTimeDiff('08:57:01', '17:23:41')); // 08:26:40
RobG
  • 142,382
  • 31
  • 172
  • 209
0

The way you are doing can be achieved in by the following code

var Intime="01:55:02";
var Outtime="05:23:01";

var IntA=Intime.split(":");

var inhh=parseInt(IntA[0])
var inmm=parseInt(IntA[1])
var inss=parseInt(IntA[2])

var outA=Outtime.split(":");

var ouhh=parseInt(outA[0])
var oumm=parseInt(outA[1])
var ouss=parseInt(outA[2])
if(ouhh>inhh)
{
 if((ouss-inss)<0)
  {
    oumm=oumm-1;
    rss=60+ouss-inss;
  }
  else
  {
     rss=ouss-inss;
  }

  if((oumm-inmm)<0)
   {
     ouhh=ouhh-1;
     rmm=60+oumm-inmm;
   }
  else
  {
     rmm=oumm-inmm;
  }

 rhh=ouhh-inhh;
 var diff=rhh+":"+rmm+":"+rss
 console.log(diff)//is the required difference

}

the fiddle for this is here

Roshan
  • 2,144
  • 2
  • 16
  • 28