JavaScript中的时间差

JavaScript中的时间差,javascript,Javascript,我发现下面的代码对于我正在使用的时间表计算器来说运行良好 function convertSecondsToHHMMSS(intSecondsToConvert) { var hours = convertHours(intSecondsToConvert); var minutes = getRemainingMinutes(intSecondsToConvert); minutes = (minutes == 60) ? "00" : minutes; var

我发现下面的代码对于我正在使用的时间表计算器来说运行良好

function convertSecondsToHHMMSS(intSecondsToConvert) {
    var hours = convertHours(intSecondsToConvert);
    var minutes = getRemainingMinutes(intSecondsToConvert);
    minutes = (minutes == 60) ? "00" : minutes;
    var seconds = getRemainingSeconds(intSecondsToConvert);
    return hours+" hrs "+minutes+" mins";
}

function convertHours(intSeconds) {
    var minutes = convertMinutes(intSeconds);
    var hours = Math.floor(minutes/60);
    return hours;
}

function convertMinutes(intSeconds) {
    return Math.floor(intSeconds/60);
}

function getRemainingSeconds(intTotalSeconds) {
    return (intTotalSeconds%60);
}

function getRemainingMinutes(intSeconds) {
    var intTotalMinutes = convertMinutes(intSeconds);
    return (intTotalMinutes%60);
}

function HMStoSec1(T) { 
  var A = T.split(/\D+/) ; return (A[0]*60 + +A[1])*60 + +A[2] 
}

var time1 = HMStoSec1("10:00:00");
var time2 = HMStoSec1("12:05:00");
var diff = time2 - time1;
document.write(convertSecondsToHHMMSS(diff));
当time1大于time2时,它可以正常工作,如果time1小于time2,则会减去额外的一小时

var time1 = HMStoSec1("09:00:00");
var time2 = HMStoSec1("08:55:00");
var diff = time2 - time1;
document.write(convertSecondsToHHMMSS(diff)); // writes "1 hr 5 mins" instead of "0 hr 5 mins"
我认为这与convertHours函数中的Math.floor有关

我正在尝试构建一个只需要几个小时和几分钟就能减去/加上时间的东西,而不是实际乘以多少小时和几分钟


很遗憾,我找不到一种更简单的方法,任何帮助都将不胜感激。

地板的工作原理不同于大多数人对负数的期望。它返回的下一个整数小于操作数,因此对于-1.5,它将返回-2。处理这个问题最简单的方法就是取绝对值(
Math.abs
),然后在末尾加上负号。

非常感谢你,现在只需要解决如何做墓地轮班:(