属于个人代码整理
filters: {
formatDate: function (e) {
// 获取js 时间戳
let time = new Date().getTime();
// 去掉 js 时间戳后三位
time = parseInt((time - e) / 1000);
// 存储转换值
let s;
if (time < 60 * 10) {
// 十分钟内
return '刚刚';
} else if (time < 60 * 60 && time >= 60 * 10) {
// 超过十分钟少于1小时
s = Math.floor(time / 60);
return s + '分钟前';
} else if (time < 60 * 60 * 24 && time >= 60 * 60) {
// 超过1小时少于24小时
s = Math.floor(time / 60 / 60);
return s + '小时前';
} else if (time < 60 * 60 * 24 * 3 && time >= 60 * 60 * 24) {
// 超过1天少于3天内
s = Math.floor(time / 60 / 60 / 24);
return s + '天前';
} else {
// 超过3天
var date = new Date(e); //时间戳为10位需*1000,时间戳为13位的话不需乘1000
var Y = date.getFullYear() + '-';
var M =
(date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
var D = date.getDate() + ' ';
var h = date.getHours() + ':';
var m = date.getMinutes() + ':';
var ss = date.getSeconds();
return Y + M + D + h + m + ss;
}
},
},

本文介绍了一个用于处理时间格式化的JavaScript函数。该函数可以根据不同的时间范围返回简洁的时间描述,如“刚刚”、“几分钟前”等,或者当时间超过三天时,则提供完整的日期和时间格式。这种处理方式常见于社交媒体和其他需要显示相对时间的应用中。
2292

被折叠的 条评论
为什么被折叠?



