2026-05-19 16:05:09 +08:00
|
|
|
import { differenceInDays, format, isToday, isYesterday, parseISO } from 'date-fns'
|
2026-07-06 10:38:41 +08:00
|
|
|
import { m } from '@/ui/paraglide/messages.js'
|
2026-05-19 16:05:09 +08:00
|
|
|
|
|
|
|
|
export const formatTimeAgo = (dateString: string) => {
|
|
|
|
|
const date = parseISO(dateString)
|
|
|
|
|
const now = new Date()
|
|
|
|
|
|
|
|
|
|
if (isNaN(date.getTime())) {
|
|
|
|
|
return dateString
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const time = format(date, 'HH:mm')
|
|
|
|
|
|
|
|
|
|
if (isToday(date)) {
|
2026-07-06 10:38:41 +08:00
|
|
|
return m.today_with_time({ time })
|
2026-05-19 16:05:09 +08:00
|
|
|
}
|
|
|
|
|
if (isYesterday(date)) {
|
2026-07-06 10:38:41 +08:00
|
|
|
return m.yesterday_with_time({ time })
|
2026-05-19 16:05:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const daysAgo = differenceInDays(now, date)
|
2026-07-06 10:38:41 +08:00
|
|
|
const count = daysAgo >= 0 ? daysAgo : 0
|
|
|
|
|
return count === 1 ? m.n_days_ago_singular({ count }) : m.n_days_ago_plural({ count })
|
2026-05-19 16:05:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const formatDateAgo = (dateString: string) => {
|
|
|
|
|
const date = parseISO(dateString)
|
|
|
|
|
const now = new Date()
|
|
|
|
|
|
|
|
|
|
if (isNaN(date.getTime())) {
|
|
|
|
|
return dateString
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isToday(date)) {
|
2026-07-06 10:38:41 +08:00
|
|
|
return m.today()
|
2026-05-19 16:05:09 +08:00
|
|
|
}
|
|
|
|
|
if (isYesterday(date)) {
|
2026-07-06 10:38:41 +08:00
|
|
|
return m.yesterday()
|
2026-05-19 16:05:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const daysAgo = differenceInDays(now, date)
|
2026-07-06 10:38:41 +08:00
|
|
|
const count = daysAgo >= 0 ? daysAgo : 0
|
|
|
|
|
return count === 1 ? m.n_days_ago_singular({ count }) : m.n_days_ago_plural({ count })
|
2026-05-19 16:05:09 +08:00
|
|
|
}
|
2026-08-17 20:24:05 +08:00
|
|
|
|
|
|
|
|
export const formatRemainingTime = (targetDate: string | Date, now: Date = new Date()): string => {
|
|
|
|
|
const targetMs =
|
|
|
|
|
typeof targetDate === 'string'
|
|
|
|
|
? parseISO(targetDate).getTime()
|
|
|
|
|
: targetDate instanceof Date
|
|
|
|
|
? targetDate.getTime()
|
|
|
|
|
: NaN
|
|
|
|
|
if (isNaN(targetMs)) {
|
|
|
|
|
return '< 1m'
|
|
|
|
|
}
|
|
|
|
|
const diffMs = targetMs - now.getTime()
|
|
|
|
|
if (diffMs <= 0) {
|
|
|
|
|
return '< 1m'
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const totalSeconds = Math.floor(diffMs / 1000)
|
|
|
|
|
const days = Math.floor(totalSeconds / 86400)
|
|
|
|
|
const hours = Math.floor((totalSeconds % 86400) / 3600)
|
|
|
|
|
const minutes = Math.floor((totalSeconds % 3600) / 60)
|
|
|
|
|
|
|
|
|
|
if (days > 0) {
|
|
|
|
|
return hours > 0 ? `${days}d ${hours}h` : `${days}d`
|
|
|
|
|
}
|
|
|
|
|
if (hours > 0) {
|
|
|
|
|
return minutes > 0 ? `${hours}h ${minutes}m` : `${hours}h`
|
|
|
|
|
}
|
|
|
|
|
return minutes > 0 ? `${minutes}m` : '< 1m'
|
|
|
|
|
}
|