Fix:3962 【商城小程序】资讯详情—评论时间超过7天显示日期

This commit is contained in:
LinCyJang
2025-07-03 22:12:31 +08:00
parent aff7cfccc0
commit 9d345d525e
+13 -19
View File
@@ -255,33 +255,27 @@ export default {
* 1. 小于1分钟,显示刚刚 * 1. 小于1分钟,显示刚刚
* 2. 小于1小时,显示xx分钟前 * 2. 小于1小时,显示xx分钟前
* 3. 小于1天,显示xx小时前 * 3. 小于1天,显示xx小时前
* 4. 小于1月,显示xx天前 * 4. 超过7天,显示日期 xxxx年xx月xx日
* 5. 小于1年,显示xx月前
* 6. 大于1年,显示xxxx年xx月xx日
* 7. 其他,显示xxxx-xx-xx
*/ */
formatterTime(time) { formatterTime(time) {
const now = new Date(); const now = new Date();
const date = new Date(time); const date = new Date(time);
const diff = now - date; // 时间差 const diff = now - date;
const diffDay = diff / (1000 * 60 * 60 * 24); // 天数 const diffMin = Math.floor(diff / 1000 / 60);
const diffHour = diff / (1000 * 60 * 60); // 小时 const diffHour = Math.floor(diff / 1000 / 60 / 60);
const diffMinute = diff / (1000 * 60); // 分钟 const diffDay = Math.floor(diff / 1000 / 60 / 60 / 24);
if (diffMinute < 1) { if (diffMin < 1) {
return '刚刚'; return "刚刚";
} else if (diffHour < 1) { } else if (diffHour < 1) {
return `${Math.floor(diffMinute)}分钟前`; return diffMin + "分钟前";
} else if (diffDay < 1) { } else if (diffDay < 1) {
return `${Math.floor(diffHour)}小时前`; return diffHour + "小时前";
} else if (diffDay < 30) { } else if (diffDay < 7) {
return `${Math.floor(diffDay)}天前`; return diffDay + "天前";
} else if (diffDay < 365) {
return `${Math.floor(diffDay / 30)}月前`;
} else if (diffDay < 365 * 10) {
return `${Math.floor(diffDay / 365)}年前`;
} else { } else {
return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`; return date.getFullYear() + "年" + (date.getMonth() + 1) + "月" + date.getDate() + "日";
} }
}, },
/** /**
* 发评论 * 发评论