如何添加小时,分钟和秒?

Nit*_*ths 1 html javascript jquery

我有两次in_time和out_time.我想用javascript添加这两次.我怎么能这样做?in_time和out_time的值来自文本框.我的HTML代码是

<div>   
<b>PunchIn</b>
    <input type="text" id="intime" class="time" placeholder="09:00:00" />
    <p class="error">PunchTime is not valid</p>
</div>
<br/>
<div>   
    <b>OutTime</b>
    <input type="text" id="brktime" class="time" placeholder="06:00:00" />
    <p class="error1">OutTime is not valid</p>
</div>
Run Code Online (Sandbox Code Playgroud)

您可以从Demo中查看代码

Guf*_*ffa 10

将时间转换为秒,添加它们并格式化结果:

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);
Run Code Online (Sandbox Code Playgroud)

演示:http://jsfiddle.net/Guffa/JLh6W/