Add month support to getRelativeTime

This commit is contained in:
csr4422
2025-10-01 16:22:02 +05:30
parent 98f16854c8
commit e4018ddab8

View File

@@ -1,16 +1,18 @@
import { format, differenceInMinutes, differenceInHours, differenceInDays, differenceInYears } from 'date-fns'
import { format, differenceInMinutes, differenceInHours, differenceInDays,differenceInMonths, differenceInYears } from 'date-fns'
export function getRelativeTime (timestamp, now = new Date()) {
try {
const mins = differenceInMinutes(now, timestamp)
const hours = differenceInHours(now, timestamp)
const days = differenceInDays(now, timestamp)
const months=differenceInMonths(now, timestamp)
const years = differenceInYears(now, timestamp)
if (mins === 0) return 'now'
if (mins < 60) return `${mins}m`
if (hours < 24) return `${hours}h`
if (days < 365) return `${days}d`
if (days < 30) return `${days}d`
if (months < 12) return `${months}mo`
return `${years}y`
} catch (error) {
console.error('Error parsing time', error, 'timestamp', timestamp)