修复了一些逻辑漏洞
This commit is contained in:
@@ -3,7 +3,8 @@
|
||||
*/
|
||||
|
||||
/**
|
||||
* 格式化时间
|
||||
* 格式化时间(相对时间)
|
||||
* 显示:刚刚、X分钟前、X小时前、昨天、X天前、具体日期
|
||||
*/
|
||||
export function formatTime(timestamp: number | string): string {
|
||||
const date = new Date(typeof timestamp === 'string' ? timestamp : timestamp)
|
||||
@@ -14,14 +15,104 @@ export function formatTime(timestamp: number | string): string {
|
||||
const hours = Math.floor(minutes / 60)
|
||||
const days = Math.floor(hours / 24)
|
||||
|
||||
if (days > 0) {
|
||||
return date.toLocaleDateString('zh-CN', { month: 'short', day: 'numeric' })
|
||||
} else if (hours > 0) {
|
||||
return `${hours}小时前`
|
||||
} else if (minutes > 0) {
|
||||
// 刚刚(1分钟内)
|
||||
if (seconds < 60) {
|
||||
return '刚刚'
|
||||
}
|
||||
|
||||
// X分钟前(1小时内)
|
||||
if (minutes < 60) {
|
||||
return `${minutes}分钟前`
|
||||
}
|
||||
|
||||
// X小时前(24小时内)
|
||||
if (hours < 24) {
|
||||
return `${hours}小时前`
|
||||
}
|
||||
|
||||
// 昨天
|
||||
const yesterday = new Date(now)
|
||||
yesterday.setDate(yesterday.getDate() - 1)
|
||||
if (date.toDateString() === yesterday.toDateString()) {
|
||||
return '昨天'
|
||||
}
|
||||
|
||||
// X天前(7天内)
|
||||
if (days < 7) {
|
||||
return `${days}天前`
|
||||
}
|
||||
|
||||
// 超过7天,显示具体日期
|
||||
const currentYear = now.getFullYear()
|
||||
const messageYear = date.getFullYear()
|
||||
|
||||
if (currentYear === messageYear) {
|
||||
// 今年:显示月-日
|
||||
return date.toLocaleDateString('zh-CN', { month: 'short', day: 'numeric' })
|
||||
} else {
|
||||
return date.toLocaleTimeString('zh-CN', { hour: '2-digit', minute: '2-digit' })
|
||||
// 去年或更早:显示年-月-日
|
||||
return date.toLocaleDateString('zh-CN', { year: 'numeric', month: 'short', day: 'numeric' })
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化消息时间(用于聊天记录)
|
||||
* 显示:刚刚、X分钟前、X小时前、昨天 HH:mm、MM-DD HH:mm、YYYY-MM-DD HH:mm
|
||||
*/
|
||||
export function formatMessageTime(timestamp: number | string): string {
|
||||
const date = new Date(typeof timestamp === 'string' ? timestamp : timestamp)
|
||||
const now = new Date()
|
||||
const diff = now.getTime() - date.getTime()
|
||||
const seconds = Math.floor(diff / 1000)
|
||||
const minutes = Math.floor(seconds / 60)
|
||||
const hours = Math.floor(minutes / 60)
|
||||
const days = Math.floor(hours / 24)
|
||||
|
||||
// 刚刚(1分钟内)
|
||||
if (seconds < 60) {
|
||||
return '刚刚'
|
||||
}
|
||||
|
||||
// X分钟前(1小时内)
|
||||
if (minutes < 60) {
|
||||
return `${minutes}分钟前`
|
||||
}
|
||||
|
||||
// X小时前(24小时内)
|
||||
if (hours < 24) {
|
||||
return `${hours}小时前`
|
||||
}
|
||||
|
||||
// 昨天 HH:mm
|
||||
const yesterday = new Date(now)
|
||||
yesterday.setDate(yesterday.getDate() - 1)
|
||||
if (date.toDateString() === yesterday.toDateString()) {
|
||||
return `昨天 ${date.toLocaleTimeString('zh-CN', { hour: '2-digit', minute: '2-digit' })}`
|
||||
}
|
||||
|
||||
// 本周内:显示星期几 HH:mm
|
||||
const weekAgo = new Date(now)
|
||||
weekAgo.setDate(weekAgo.getDate() - 7)
|
||||
if (date > weekAgo) {
|
||||
const weekdays = ['日', '一', '二', '三', '四', '五', '六']
|
||||
const weekday = weekdays[date.getDay()]
|
||||
return `周${weekday} ${date.toLocaleTimeString('zh-CN', { hour: '2-digit', minute: '2-digit' })}`
|
||||
}
|
||||
|
||||
// 今年:MM-DD HH:mm
|
||||
const currentYear = now.getFullYear()
|
||||
const messageYear = date.getFullYear()
|
||||
|
||||
if (currentYear === messageYear) {
|
||||
const month = (date.getMonth() + 1).toString().padStart(2, '0')
|
||||
const day = date.getDate().toString().padStart(2, '0')
|
||||
return `${month}-${day} ${date.toLocaleTimeString('zh-CN', { hour: '2-digit', minute: '2-digit' })}`
|
||||
} else {
|
||||
// 去年或更早:YYYY-MM-DD HH:mm
|
||||
const year = date.getFullYear()
|
||||
const month = (date.getMonth() + 1).toString().padStart(2, '0')
|
||||
const day = date.getDate().toString().padStart(2, '0')
|
||||
return `${year}-${month}-${day} ${date.toLocaleTimeString('zh-CN', { hour: '2-digit', minute: '2-digit' })}`
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user