/*获取时间差*/
function getDifferTime(targetDate){
const nowDate=new Date();
const differTime=new Date(nowDate.getTime()-targetDate.getTime());
const differDays=parseInt(differTime/(1000*60*60*24));
const differYear=parseInt(differDays/365);
const differMonth=parseInt(differDays/30);
const differHours=nowDate.getHours()-targetDate.getHours();
const differMinutes=Math.abs(nowDate.getMinutes()-targetDate.getMinutes());
const differAry=[{
dateDes:`${differYear}年前`,
dateVal:differYear
},{
dateDes:`${differMonth}月前`,
dateVal:differMonth
},{
dateDes:`${differDays}日前`,
dateVal:differDays
},{
dateDes: `${differHours>0?`${differHours}小时${differMinutes}分前`:`${differMinutes}分钟前`}`,
dateVal:differHours>0?differHours:differMinutes
}];
return differAry.find((item)=>{
return item.dateVal>0;
})||'刚刚';
}
console.log(JSON.stringify(getDifferTime(new Date('2020/4/3 12:00'))));
console