let dayTime = '2022-05-15 19:47' // 年
let timeVal = new Date(dayTime.replace(/-/g, "/")).getTime();
//时间戳时间格式化(毫秒)
function dates(timeStamp) {
let second = 1000;
let minute = second * 60;
let hour = minute * 60;
let day = hour * 24;
let month = day * 30;
let now = new Date().getTime();
let nowHouer = new Date().getHours() // 当前时间的小时值
let diffValue = now - timeStamp;
let monthC = diffValue / month; //月
let weekC = diffValue / (7 * day); //周
let dayC = diffValue / day; //天
let hourC = diffValue / hour; //时
let minC = diffValue / minute; //分
let secC = diffValue / second; //秒
if (monthC >= 12) {
let data = new Date(parseInt(timeStamp));
let year = data.getFullYear();
let month = data.getMonth() + 1;
let days = data.getDate() < 10 ? '0' + data.getDate() : '' + data.getDate();
return `${year}年${month}月${days}日`
}
if(hourC > 48){
let data = new Date(parseInt(timeStamp));
let year = data.getFullYear();
let month = data.getMonth() + 1;
let days = data.getDate() < 10 ? '0' + data.getDate() : '' + data.getDate();
return `${month}月${days}日`
}
if(hourC > 24 && hourC <= 48){
return (hourC - nowHouer) / 24 > 1 ? '前天' : '昨天'
}
if(hourC >= 1 && hourC <= 24){
return `${parseInt(hourC)}小时前`
}
if(minC >= 2 && hourC < 1){
return `${parseInt(minC)}分钟前`
}
if(minC < 2){
return "1分钟前"
}
}
console.log(dates(timeVal))
06-27
1398