js计算时间差代码

JavaScript08

js计算时间差代码,第1张

js求时间差

var date1=new Date() //开始时间

alert("aa")

var date2=new Date() //结束时间

var date3=date2.getTime()-date1.getTime() //时间差的毫秒数

//计算出相差天数

var days=Math.floor(date3/(24*3600*1000))

转为 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 )//两个时间相差的小时数