Skip to content

@pmun/utils / formatChatTime

Function: formatChatTime()

ts
function formatChatTime(date): string;

格式化日期为聊天列表时间格式(类似微信聊天列表)

Parameters

date

DateLike

日期

Returns

string

格式化后的时间字符串

  • 当天:HH:mm
  • 昨天:昨天 HH:mm
  • 7天内(不包括今天和昨天):星期几
  • 今年其他日期:MM月DD日
  • 往年日期:YYYY年MM月DD日

Example

ts
formatChatTime(new Date()) // "14:30"
formatChatTime(new Date(Date.now() - 24 * 60 * 60 * 1000)) // "昨天 09:15"
formatChatTime(new Date(Date.now() - 3 * 24 * 60 * 60 * 1000)) // "星期三"
formatChatTime('2023-05-15') // "05月15日"
formatChatTime('2022-12-25') // "2022年12月25日"