废话少说,上代码。
const minute = 1000 * 60
const hour = minute * 60
const day = hour * 24
// const halfamonth = day * 15
const month = day * 30
const timeAgo = time => {
const dateTimeStamp = Date.parse(time);
const now = new Date().getTime()
const diffValue = now - dateTimeStamp
let result = '';
if (diffValue < 0) {
return;
}
const monthC = diffValue / month
// console.log(monthC)
// var weekC = diffValue / (7 * day)
const dayC = diffValue / day
const hourC = diffValue / hour
const minC = diffValue / minute
if (monthC >1) {
result = time
} 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
}
export { timeAgo }
传入时间对象即可
要是文章代码高亮行号显示就好了,问一句。作者打不打算将博客开源