這是JavaScript中,第二計算還包括
試試這個,
var a = "17.30.15hrs"
var b = "1.30.20hrs"
alert(addtime(a, b));
var a = "23.00.25hrs"
var b = "1.30.60hrs"
alert(addtime(a, b));
function addtime(start_time, end_time) {
var startArr = start_time.replace('hrs', '', start_time).split('.');
var endArr = end_time.replace('hrs', '', end_time).split('.');
var d = new Date();
startArr[0] = (startArr[0]) ? parseInt(startArr[0], 10) : 0;
startArr[1] = (startArr[1]) ? parseInt(startArr[1], 10) : 0;
startArr[2] = (startArr[2]) ? parseInt(startArr[2], 10) : 0;
endArr[0] = (endArr[0]) ? parseInt(endArr[0], 10) : 0;
endArr[1] = (endArr[1]) ? parseInt(endArr[1], 10) : 0;
endArr[2] = (endArr[2]) ? parseInt(endArr[2], 10) : 0;
d.setHours(startArr[0] + endArr[0]);
d.setMinutes(startArr[1] + endArr[1]);
d.setSeconds(startArr[2] + endArr[2]);
var hours = d.getHours();
var minutes = d.getMinutes();
var seconds = d.getSeconds();
return hours + '.' + minutes + '.' + seconds + 'hrs';
}
演示:http://jsfiddle.net/AXVPf/7/
更新與第二計算
你使用24小時制或AM/PM後綴? – 2013-04-08 12:21:49
其12小時時鐘 – Niths 2013-04-09 04:22:39