时间格式化
const formatDate = (value) => {
const time = Date.now() - new Date(value)
const times = [31536000000, 2592000000, 86400000, 3600000, 60000, 1000]
const units = ['年前', '月前', '天前', '小时前', '分钟前', '秒前']
for (let i = 0; i < times.length; i++) {
这个是我们用我们得到多久前的时间去除于我们计算好的一年地时间去判断我们发布时间在什么范围(在年前还是月前或是天前)
const res = time / times[i]
if (res >= 1) {
return Math.round(res) + units[i]
Math.round的作用是四舍五如取整,units[i]的意思是我们显示是那个时间范围前
}
}
————————————————
版权声明:本文为CSDN博主「h周杰偷」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_58467680/article/details/118615864