<!-- div --> <view>{{countRunTime(fromTime,toTime,'hh时mm分')" }}</view> <!-- script --> countRunTime(date1, date2, pattern) { // date1 开始时间, date2 结束时间, pattern 显示格式(这里只需要显示经时时分秒格式) let startTime = new Date(date1) let endTime = new Date(date2) var dataDiff = endTime.getTime() - startTime.getTime() //天数 var dayDiff = Math.floor(dataDiff / (24 * 3600 * 1000)) //小时 var leave1 = dataDiff % (24 * 3600 * 1000) var hours = Math.floor(leave1 / (3600 * 1000)) //分钟 var leave2 = leave1 % (3600 * 1000) var minutes = Math.floor(leave2 / (60 * 1000)) //秒数 var leave3 = leave2 % (60 * 1000) var seconds = Math.round(leave3 / 1000) if (pattern.indexOf(":") != -1) { return hours + ":" + minutes } else { return hours + '小时' + minutes + '分' } }
判断相差天数
//计算天数 countDays(date1, date2) { //date1 开始时间, date2 结束时间, 将日期字符串转换为时间戳(单位:毫秒 ) var timestamp1 = new Date(date1).getTime(); var timestamp2 = new Date(date2).getTime(); // 计算两个时间戳之间的毫秒数差值 var diffInMillisecs = Math.abs(timestamp2 - timestamp1); // 将毫秒数差值转换为天数 var daysDiff = Math.ceil(diffInMillisecs / (1000 * 60 * 60 * 24)); return daysDiff }
标签:date1,date2,--,getTime,时间,var,时分秒,Math,1000 From: https://www.cnblogs.com/ayuaichiyu/p/18176780