Question

I would like to add two time variables to each other. but the output is not what i want. Because when i set for example :

starttimestatus = 13:00:00
endduur = 13:30:00

My endtimestatus will be => 13:13:30

Somehow it doesnt add the HH

  starttimestatus = $("#starttimestatus").val(),
 endduur = $("#endduur").val();
      endtimestatus = endduur + starttimestatus,
Was it helpful?

Solution

You can just add the times together

function goodTimes() {
    var arr = [];
    $.each(arguments, function() {
        $.each(this.split(':'), function(i) {
            arr[i] = arr[i] ? arr[i] + (+this) : +this;
        });
    })

    return arr.map(function(n) {
        return n < 10 ? '0'+n : n;
    }).join(':');
}

FIDDLE

OTHER TIPS

You need convert the times to seconds, add them, and format the result.

function toSeconds(s) {
  var p = s.split(':');
  return parseInt(p[0], 10) * 3600 + parseInt(p[1], 10) * 60 + parseInt(p[2], 10);
}

function fill(s, digits) {
  s = s.toString();
  while (s.length < digits) s = '0' + s;
  return s;
}

var sec = toSeconds(intime) + toSeconds(out);

var result =
  fill(Math.floor(sec / 3600), 2) + ':' +
  fill(Math.floor(sec / 60) % 60, 2) + ':' +
  fill(sec % 60, 2);

Refer to fiddle for Demo

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top