方法源码
export function timestamp(datetime: string): string {
const timestamp = new Date(datetime).getTime()
const diff = Date.now() - timestamp
const minute = 60 * 1000
const hour = 60 * minute
const day = 24 * hour
const week = 7 * day
const month = 30 * day
const year = 365 * day
if (diff < minute) {
return '刚刚'
} else if (diff < hour) {
const minutes = Math.floor(diff / minute)
return `${minutes} 分钟前`
} else if (diff < day) {
const hours = Math.floor(diff / hour)
return `${hours} 小时前`
} else if (diff < week) {
const days = Math.floor(diff / day)
return `${days} 天前`
} else if (diff < month) {
const weeks = Math.floor(diff / week)
return `${weeks} 周前`
} else if (diff < year) {
const months = Math.floor(diff / month)
return `${months} 个月前`
} else {
const years = Math.floor(diff / year)
return `${years} 年前`
}
}
使用示例
timestamp(2023-5-27 10:20:55)
发表评论- Comments
您必须登录或注册发表评论