This commit is contained in:
oneao committed 2025-12-04 17:31:33 +08:00
1 parent 4dcac7b5a0
commit 59d3be2299
25 files changed
+380 -125

No files matched your search

@@ -68,3 +68,40 @@ export function isInMonth(date: string, month: string): boolean {
// month 形如 "2025-01"
return dayjs(date).format('YYYY-MM') === month
}
export function formatRelativeTime(date: string | Date | number): string {
if (typeof date === 'string' && !date.trim()) {
return '暂无活跃'
}
const d = dayjs(date)
if (!d.isValid())
return '暂无活跃'
const now = dayjs()
const diff = now.diff(d, 'second') // 相差秒数
if (diff < 60) {
return '刚刚'
}
else if (diff < 3600) {
const minutes = Math.floor(diff / 60)
return `${minutes}分钟前`
}
else if (diff < 86400) {
const hours = Math.floor(diff / 3600)
return `${hours}小时前`
}
else if (diff < 2592000) { // 30天以内
const days = Math.floor(diff / 86400)
return `${days}天前`
}
else if (diff < 31536000) { // 1年以内
const months = Math.floor(diff / 2592000)
return `${months}个月前`
}
else {
const years = Math.floor(diff / 31536000)
return `${years}年前`
}
}