复制即用
![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/04b4e60729fb4e58ba715eb3e2048517.png)
console.log(Tools('2024-07-25 18:00:00'))
function Tools(StartDate) {
let data = new Date(StartDate);
let startTamp = data.getTime()
let result;
let minute = 1000 * 60;
let hour = minute * 60;
let day = hour * 24;
let month = day * 30;
let now = new Date().getTime();
let diffValue = now - startTamp;
if(diffValue < 0){
return;
}
let monthC =diffValue/month;
let weekC =diffValue/(7*day);
let dayC =diffValue/day;
let hourC =diffValue/hour;
let minC =diffValue/minute;
if(monthC>=1){
if(monthC<=12)
result="" + parseInt(monthC) + "月前";
else{
result="" + parseInt(monthC/12) + "年前";
}
}
else if(weekC>=1){
result="" + parseInt(weekC) + "周前";
}
else if(dayC>=1){
result=""+ parseInt(dayC) +"天前";
}
else if(hourC>=1){
result=""+ parseInt(hourC) +"小时前";
}
else if(minC>=1){
result=""+ parseInt(minC) +"分钟前";
}else{
result="刚刚";
}
return result;
}