1.在评论类需求会遇到 几分钟,几小时,几天前评论 0和1单位不加s,大于1加s显示复数:minutes
function pluralize(time, label) { // 时间戳以秒算, 分钟,小时,天数
if (time === 1) {
return time + label
}
return time + label + 's'
}
export function timeAgo(time) {
const between = Date.now() / 1000 - Number(time)
if (between < 3600) {
return pluralize(~~(between / 60), ' minute') // ~~ 运算符 把非数字类型转为数字类型 ~~123 == 123 是不变的
} else if (between < 86400) {
return pluralize(~~(between / 3600), ' hour')
} else {
return pluralize(~~(between / 86400), ' day')
}
}