Files
workspace/code/link56/link56-frontend/src/utils/time.ts
T
2026-04-24 17:28:11 +08:00

168 lines
3.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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日')
}
export function formatCommentTime(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 HH:mm')
}
// 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 < 7) {
return `${diffDays}天前`
}
// 本年
if (time.year() === now.year()) {
return time.format('MM-DD HH:mm')
}
// 非本年
return time.format('YYYY-MM-DD HH:mm')
}
type DateFormat
= | 'YYYY'
| 'MM'
| 'DD'
| 'YYYY-MM'
| 'YYYY-MM-DD'
| 'YYYY-MM-DD HH:mm'
| 'YYYY-MM-DD HH:mm:ss'
| 'YYYY年MM月DD日'
export function formatDate(
input?: dayjs.ConfigType,
format: DateFormat = 'YYYY-MM-DD',
): string {
if (!input)
return ''
const date = dayjs(input)
if (!date.isValid())
return ''
return date.format(format)
}
/**
* 格式化聊天时间
* @param timestamp - 毫秒级时间戳
* @param mode - 显示模式:'list' 为列表,'chat' 为聊天记录
* @returns 格式化后的时间字符串
*/
export function formatImTime(timestamp: number | null | undefined, mode: 'list' | 'chat' = 'list'): string {
if (!timestamp)
return ''
const now = new Date()
const date = new Date(timestamp)
const hour = String(date.getHours()).padStart(2, '0')
const minute = String(date.getMinutes()).padStart(2, '0')
const weekDays = ['周日', '周一', '周二', '周三', '周四', '周五', '周六']
// 去除时分秒的日期
const nowMidnight = new Date(now.getFullYear(), now.getMonth(), now.getDate())
const dateMidnight = new Date(date.getFullYear(), date.getMonth(), date.getDate())
const diffDays = Math.floor((nowMidnight.getTime() - dateMidnight.getTime()) / (1000 * 3600 * 24))
// 1️⃣ 当天
if (diffDays === 0)
return `${hour}:${minute}`
// 2️⃣ 昨天
if (diffDays === 1)
return `昨天 ${hour}:${minute}`
// 3️⃣ 一周内(周一到周日)
const weekStart = new Date(nowMidnight.getTime() - ((now.getDay() || 7) - 1) * 24 * 3600 * 1000)
if (dateMidnight >= weekStart) {
return mode === 'chat'
? `${weekDays[date.getDay()]} ${hour}:${minute}`
: weekDays[date.getDay()]
}
// 4️⃣ 本年或跨年(聊天记录模式:统一显示 YYYY/MM/DD HH:mm)
const ymd = `${date.getFullYear()}/${String(date.getMonth() + 1).padStart(2, '0')}/${String(date.getDate()).padStart(2, '0')}`
if (mode === 'chat')
return `${ymd} ${hour}:${minute}`
// 列表模式区分本年/跨年
if (date.getFullYear() === now.getFullYear())
return `${String(date.getMonth() + 1).padStart(2, '0')}/${String(date.getDate()).padStart(2, '0')}`
return ymd
}