This commit is contained in:
oneao committed 2025-12-26 17:30:43 +08:00
1 parent 4f5218c892
commit 2f8a815c62
32 files changed
+1915 -1738

No files matched your search

@@ -0,0 +1,46 @@
import dayjs from 'dayjs'
export function formatPostTime(input?: dayjs.ConfigType): string {
if (!input)
return ''
const time = dayjs(input)
if (!time.isValid())
return ''
const now = dayjs()
const diffSeconds = now.diff(time, 'second')
if (diffSeconds < 0) {
// 未来时间兜底
return time.format('YYYY年MM月DD日')
}
// 1 分钟内
if (diffSeconds < 60) {
return '刚刚'
}
const diffMinutes = now.diff(time, 'minute')
if (diffMinutes < 60) {
return `${diffMinutes}分钟前`
}
const diffHours = now.diff(time, 'hour')
if (diffHours < 24) {
return `${diffHours}小时前`
}
const diffDays = now.diff(time, 'day')
if (diffDays < 14) {
return `${diffDays}天前`
}
// 本年
if (time.year() === now.year()) {
return time.format('MM月DD日')
}
// 非本年
return time.format('YYYY年MM月DD日')
}