Browse Source

fix: 修复未来时间导致的时间差计算负值问题

当消息时间戳为未来时间时,now - msgTime 会得到负值,导致后续时间计算异常。通过将负差值强制设为0,确保时间差始终为非负数。
milestone-20260128-日常优化1.0
zhangrenyuan 3 weeks ago
parent
commit
1798d3d49d
  1. 5
      src/utils/getMessage.js

5
src/utils/getMessage.js

@ -15,7 +15,10 @@ function formatTime(timeStr) {
// 函数逻辑不变... // 函数逻辑不变...
const now = new Date(); const now = new Date();
const msgTime = new Date(timeStr); const msgTime = new Date(timeStr);
const diffMs = now - msgTime;
let diffMs = now - msgTime;
if (diffMs < 0) {
diffMs = 0
}
const diffMins = Math.floor(diffMs / (1000 * 60)); const diffMins = Math.floor(diffMs / (1000 * 60));
const diffHours = Math.floor(diffMins / 60); const diffHours = Math.floor(diffMins / 60);
const diffDays = Math.floor(diffHours / 24); const diffDays = Math.floor(diffHours / 24);

Loading…
Cancel
Save