本文主要是介绍计算时间差案例,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
var time1 = new Date('2019-01-01 00:00:00')var time2 = new Date('2019-01-03 04:55:34')var t1 = time1.getTime() //第一天的毫秒var t2 = time2.getTime() //第n天的毫秒var t3 = t2 - t1 //相隔多少毫秒// var day1 = t3 / (60*60*24*1000) //求隔了多少天// day1 = Math.floor(day1)// console.log(day1) // 2天 // var hourss = t3 - day1* (60*60*24*1000) //小时的毫秒数// var hours1 = hourss / (60*60*1000) //小时毫秒转换 小时 // var hours2 = Math.floor(hours1) //小时取整// console.log(hours2) //2小时// var minuess = hourss - hours2 * (60*60*1000)var t1 = time1.getTime()var t2 = time2.getTime()var t3 = t2 - t1 // 相差毫秒数// 相差的天,小时,分钟,秒// 1天= 24小时 1小时= 60分钟 1分钟 = 60秒 1s = 1000毫秒// 1天 = 24 * 60 * 60 * 1000var day1 = t3 / (24 * 60 * 60 * 1000)day1 = Math.floor(day1)console.log(day1);// 总相差毫秒数 - 相差天数的毫秒数 = 剩下相差小时分钟秒 毫秒数var hourseSeconds = t3 - day1 * (24 * 60 * 60 * 1000) var hourse = hourseSeconds/(60*60*1000)hourse = Math.floor(hourse)console.log(hourse);//分钟和秒 毫秒数 = hourseSeconds - hourse * 60*60*1000var minuesSeconds = hourseSeconds - hourse * 60*60*1000var minue = minuesSeconds/(60*1000)minue = Math.floor(minue)console.log(minue);
出生到现在
<!-- 求你出生到现在经历多少天多少小时?--><script>var time1 = new Date('1999-03-09 00:00:00')var time2 = new Date('2021-12-02 20:25:00')//获取毫秒var ms1 = time1.getTime()var ms2 = time2.getTime() var ms3 = ms2 - ms1 //多少天var day = ms3 / (60*60*24*1000) day = Math.floor(day)console.log(day)//多少小时var hours = ms3 - day *(60*60*24*1000)hourss = hours / (60*60*1000)console.log(hourss)
这篇关于计算时间差案例的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!