1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| formatTime(timestamp) { const now = new Date().getTime(); const diff = (now - timestamp) / 1000;
if (diff < 1) { return '刚刚'; } else if (diff < 60) { return `${Math.floor(diff)}秒钟前`; } else if (diff < 3600) { return `${Math.floor(diff / 60)}分钟前`; } else { const date = new Date(timestamp); const year = date.getFullYear(); const month = String(date.getMonth() + 1).padStart(2, '0'); const day = String(date.getDate()).padStart(2, '0'); const hours = String(date.getHours()).padStart(2, '0'); const minutes = String(date.getMinutes()).padStart(2, '0'); const seconds = String(date.getSeconds()).padStart(2, '0');
const today = new Date(); const yesterday = new Date(today); yesterday.setDate(yesterday.getDate() - 1);
if (date.toDateString() === today.toDateString()) { return `今天 ${hours}:${minutes}:${seconds}`; } else if (date.toDateString() === yesterday.toDateString()) { return `昨天 ${hours}:${minutes}:${seconds}`; } else if (year === today.getFullYear()) { return `${month}-${day} ${hours}:${minutes}:${seconds}`; } else { return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`; } } }
|