整理了下,满足日常使用
function formatDate(date) {
const mistiming = Math.round((Date.now() - new Date(date).getTime()) / 1000)
const tags = ['年', '个月', '星期', '天', '小时', '分钟', '秒']
const times = [31536000, 2592000, 604800, 86400, 3600, 60, 1]
for (let i = 0; i < times.length; i++) {
const inm = Math.floor(mistiming / times[i])
if (tags[i] === '天') {
switch (inm) {
case 0:
return '今天'
break
case 1:
return '昨天'
break
case 2:
return '前天'
break
default:
return inm + tags[i] + '前'
break;
}
}
if (inm !== 0) {
return inm + tags[i] + '前'
}
}
}