按照你的要求编写的计算时间差的Javascript程序如下(注意 date是随意写的正确的日期字符串,只是为了函数Date.parse()的需要)
<script type=text/javascript>var date="2018/07/17"
var time1 = Date.parse(date+" "+"2:32:39")
var time2 = Date.parse(date+" "+"14:12:09")
var time3=time2-time1
var hour=Math.floor(time3/1000/60/60)
var minute=Math.floor(time3/1000/60-hour*60)
var second=time3/1000-hour*60*60-minute*60
alert("时间差为"+hour+"小时"+minute+"分"+second+"秒")
</script>
运行结果
时间差为11小时39分30秒
转为 DATE() 类型 直接相减 得出毫秒数 除1000 得秒数 除60000 得分钟数
var d1 = new Date('2016/03/28 10:17:22')var d2 = new Date('2016/03/28 11:17:22')
console.log(parseInt(d2 - d1))//两个时间相差的毫秒数
console.log(parseInt(d2 - d1) / 1000)//两个时间相差的秒数
console.log(parseInt(d2 - d1) / 6000 )//两个时间相差的分钟数
console.log(parseInt(d2 - d1) / 3600000 )//两个时间相差的小时数
//+---------------------------------------------------//| 求两个时间的天数差 日期格式为 YYYY-MM-dd
//+---------------------------------------------------
function daysBetween(DateOne,DateTwo)
{
var OneMonth = DateOne.substring(5,DateOne.lastIndexOf ('-'))
var OneDay = DateOne.substring(DateOne.length,DateOne.lastIndexOf ('-')+1)
var OneYear = DateOne.substring(0,DateOne.indexOf ('-'))
var TwoMonth = DateTwo.substring(5,DateTwo.lastIndexOf ('-'))
var TwoDay = DateTwo.substring(DateTwo.length,DateTwo.lastIndexOf ('-')+1)
var TwoYear = DateTwo.substring(0,DateTwo.indexOf ('-'))
var cha=((Date.parse(OneMonth+'/'+OneDay+'/'+OneYear)- Date.parse(TwoMonth+'/'+TwoDay+'/'+TwoYear))/86400000)
return Math.abs(cha)
}