mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-22 19:27:36 +00:00
42 lines
888 B
TypeScript
42 lines
888 B
TypeScript
import { differenceInDays, format, isToday, isYesterday, parseISO } from 'date-fns'
|
|
|
|
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)) {
|
|
return `Today ${time}`
|
|
}
|
|
if (isYesterday(date)) {
|
|
return `Yesterday ${time}`
|
|
}
|
|
|
|
const daysAgo = differenceInDays(now, date)
|
|
return `${daysAgo >= 0 ? daysAgo : 0} days ago`
|
|
}
|
|
|
|
export const formatDateAgo = (dateString: string) => {
|
|
const date = parseISO(dateString)
|
|
const now = new Date()
|
|
|
|
if (isNaN(date.getTime())) {
|
|
return dateString
|
|
}
|
|
|
|
if (isToday(date)) {
|
|
return 'Today'
|
|
}
|
|
if (isYesterday(date)) {
|
|
return 'Yesterday'
|
|
}
|
|
|
|
const daysAgo = differenceInDays(now, date)
|
|
return `${daysAgo >= 0 ? daysAgo : 0} days ago`
|
|
}
|