计算时间差(小时和分钟) var t1 = '08:30'; var t2 = '20:00'; var darr1 = t1.split(':'); var darr2 = t2.split(':'); var h1 = parseInt(darr1[0]); var h2 = parseInt(darr2[0]); if (h1 > h2) { // 前面时间大于后面,说明是隔天-下面日期写死就行,作用就是取隔天的 t1 = '2022-12-27 ' + t1; t2 = '2022-12-28 ' + t2; } else { t1 = '2022-12-27 ' + t1; t2 = '2022-12-27 ' + t2; } var d1 = new Date(t1); //已知时间戳 var d2 = new Date(t2); //当前时间戳 var minute = String(parseInt(d2 - d1) / 1000 / 3600) //算出相差的小时 console.log(minute.substring(0, 5), 'minute'); //11.5 根据时间戳转标准时间 var t1 = 2023-03-08 16:42:13 var t2 = 2023-03-08 16:40:47 var d1 = new Date(t1); //已知时间戳 var d2 = new Date(t2); //当前时间戳 timeChange(parseInt(d2 - d1)) functiontimeChange(time) { var date = new Date(time);//时间戳为10位需*1000,时间戳为13位的话不需乘1000 var Y = date.getFullYear() + '-'; var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-'; var D = date.getDate() + ' '; var h = date.getHours() + ':'; var m = date.getMinutes() < 10 ? '0'+date.getMinutes() + ':' : date.getMinutes() + ':'; var s = date.getSeconds(); console.log(Y+M+D+h+m+s,'Y+M+D+h+m+s') return Y+M+D+h+m+s; } getLocalTime(parseInt(d2 - d1)) function getLocalTime(time) { var date = new Date(time + 8 * 3600*1000 ); console.log(date.toJSON().substr(0, 19).replace('T', ' ')) return date.toJSON().substr(0, 19).replace('T', ' '); }