var time1 = new Date("2021-04-16 00:00"); var time2 = new Date().getTime(); console.log(time1); console.log(time2); // 2021-04-16 39:30 var diff = time2 -time1; var hour = diff/(1000*60); console.log(hour);
还有一种结果是负的
注意点,2个时间虽然不是同格式,但是也能相减。
上面的计算结果是,相关几分,如果算几个小时,再*60
var hour = diff/(1000*60*60);
站长微信:xiaomao0055
站长QQ:14496453