计算两个时间相差的小时数
发布网友
发布时间:2022-05-23 14:54
我来回答
共2个回答
热心网友
时间:2023-10-26 01:49
var hour=a.substring(16,2)-b.substring(16,2)
a为时间(1) b为时间(2)
热心网友
时间:2023-10-26 01:50
<script language="javascript">
Date.prototype.dateDiff = function(interval,objDate){
//若参数不足或 objDate 不是日期物件则回传 undefined
if(arguments.length<2||objDate.constructor!=Date) return undefined;
switch (interval) {
//计算秒差
case "s":return parseInt((objDate-this)/1000);
//计算分差
case "n":return parseInt((objDate-this)/60000);
//计算时差
case "h":return parseInt((objDate-this)/3600000);
//计算日差
case "d":return parseInt((objDate-this)/86400000);
//计算周差
case "w":return parseInt((objDate-this)/(86400000*7));
//计算月差
case "m":return (objDate.getMonth()+1)+((objDate.getFullYear()-this.getFullYear())*12)-(this.getMonth()+1);
//计算年差
case "y":return objDate.getFullYear()-this.getFullYear();
//输入有误
default:return undefined;
}
}
</script>
<script language="javascript">
var sDT = new Date("2008/03/21 08:00:00");
var eDT = new Date("2008/03/22 08:00:00");
document.writeln("时差 : "+sDT.dateDiff("h",eDT)+"<br>");
</script>