formatImChatConversationListTime
会话列表等场景的相对时间展示(刚刚 / N 分钟前 / 本地日期)。
函数签名
typescript
function formatImChatConversationListTime(timestamp?: Date): string参数
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
timestamp | Date | 否 | 会话最后消息时间;缺省或无效时返回空字符串 |
返回值
| 类型 | 说明 |
|---|---|
string | 格式化后的展示文案 |
展示规则:
| 时间差 | 文案 |
|---|---|
| < 1 分钟 | 刚刚 |
| < 1 小时 | N分钟前 |
| < 24 小时 | N小时前 |
| < 7 天 | N天前 |
| ≥ 7 天 | timestamp.toLocaleDateString() 本地日期 |
工作原理
- 用
new Date()与入参时间戳计算毫秒差。 - 按上表阈值依次判断,返回对应中文相对时间。
- 超过 7 天则降级为浏览器本地化日期字符串(依赖运行环境
toLocaleDateString)。