js判断二个时间相差几个小时
// 当前时间
function getnyr(){
const year = new Date().getFullYear();
const month = (new Date().getMonth() + 1).toString().padStart(2, "0");
const day = new Date().getDate().toString().padStart(2, "0");
const hour = new Date().getHours().toString().padStart(2, "0");
const minutes = new Date().getMinutes().toString().padStart(2, "0");
const seconds = new Date().getSeconds().toString().padStart(2, "0");
return year + "-" + month + "-" + day+' '+hour + ":" + minutes + ":" + seconds;
}
function isIntervalOverTwoHours(time1:any, time2:any) {
const date1:Date = new Date(time1);
const date2:Date = new Date(time2);
const diff:number = Math.abs(date1.getTime() - date2.getTime());
return Math.floor(diff / (1000 * 60 * 60));
}
let currentnyr:string = getnyr()
let rownyr:string = "时间";
let isTwo = isIntervalOverTwoHours(rownyr, currentnyr)
console.log(isTwo) // 几小时