数据分析ui补充完善,接入数据库
This commit is contained in:
@@ -173,3 +173,29 @@ export function setClipboard(text: string): void {
|
||||
// #endif
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化时间,显示为相对时间(如:刚刚,几小时前)
|
||||
* @param dateStr ISO 格式的日期字符串
|
||||
* @returns 格式化后的相对时间字符串
|
||||
*/
|
||||
export function formatTime(dateStr: string): string {
|
||||
if (!dateStr) return ''
|
||||
try {
|
||||
const date = new Date(dateStr)
|
||||
const now = new Date()
|
||||
const diff = now.getTime() - date.getTime()
|
||||
const hours = Math.floor(diff / (1000 * 60 * 60))
|
||||
|
||||
if (hours < 1) {
|
||||
return '刚刚'
|
||||
} else if (hours < 24) {
|
||||
return `${hours}小时前`
|
||||
} else {
|
||||
return `${Math.floor(hours / 24)}天前`
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('formatTime error:', e)
|
||||
return dateStr.replace('T', ' ').split('.')[0]
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user