设备状态ui提交
parent
4b0db12a76
commit
5df6f6ff84
|
|
@ -78,43 +78,46 @@ export default {
|
|||
]
|
||||
}
|
||||
},
|
||||
onShow() {
|
||||
this.getSortDate();
|
||||
},
|
||||
methods: {
|
||||
// 核心时间格式化方法
|
||||
formatTime(timeStr) {
|
||||
const now = new Date();
|
||||
const target = new Date(timeStr);
|
||||
const nowTime = now.getTime();
|
||||
const targetTime = target.getTime();
|
||||
const diffTime = nowTime - targetTime;
|
||||
const oneDay = 24 * 60 * 60 * 1000;
|
||||
const sevenDays = 7 * oneDay;
|
||||
const diff = now - target;
|
||||
const day = 86400000;
|
||||
const week = 7 * day;
|
||||
|
||||
// 格式化时分秒
|
||||
const hh = String(target.getHours()).padStart(2, '0');
|
||||
const mm = String(target.getMinutes()).padStart(2, '0');
|
||||
const timeSuffix = `${hh}:${mm}`;
|
||||
// 时间
|
||||
const h = String(target.getHours()).padStart(2, '0');
|
||||
const m = String(target.getMinutes()).padStart(2, '0');
|
||||
const time = `${h}:${m}`;
|
||||
|
||||
// 今天
|
||||
if (now.toDateString() === target.toDateString()) {
|
||||
return `今天 ${timeSuffix}`;
|
||||
}
|
||||
if (now.toDateString() === target.toDateString())
|
||||
return `今天 ${time}`;
|
||||
|
||||
// 昨天
|
||||
const yesterday = new Date(now);
|
||||
yesterday.setDate(now.getDate() - 1);
|
||||
if (yesterday.toDateString() === target.toDateString()) {
|
||||
return `昨天 ${timeSuffix}`;
|
||||
}
|
||||
// 7天内(含昨天)
|
||||
if (diffTime <= sevenDays) {
|
||||
const yest = new Date(now);
|
||||
yest.setDate(now.getDate() - 1);
|
||||
if (yest.toDateString() === target.toDateString())
|
||||
return `昨天 ${time}`;
|
||||
|
||||
// 7天内
|
||||
if (diff <= week) {
|
||||
const weekMap = ['日', '一', '二', '三', '四', '五', '六'];
|
||||
const week = weekMap[target.getDay()];
|
||||
return `星期${week} ${timeSuffix}`;
|
||||
return `星期${weekMap[target.getDay()]} ${time}`;
|
||||
}
|
||||
// 7天以前
|
||||
const y = target.getFullYear();
|
||||
const m = String(target.getMonth() + 1).padStart(2, '0');
|
||||
const d = String(target.getDate()).padStart(2, '0');
|
||||
return `${y}-${m}-${d} ${timeSuffix}`;
|
||||
|
||||
// 7天前
|
||||
const Y = target.getFullYear();
|
||||
const M = String(target.getMonth() + 1).padStart(2, '0');
|
||||
const D = String(target.getDate()).padStart(2, '0');
|
||||
return `${Y}-${M}-${D} ${time}`;
|
||||
},
|
||||
getSortDate() {
|
||||
return this.messageList.sort((a, b) => new Date(a.time) - new Date(b.time));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue