mirror of
https://github.com/abhinavxd/libredesk.git
synced 2025-10-23 05:11:57 +00:00
Add month support to getRelativeTime
This commit is contained in:
@@ -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)
|
||||
|
Reference in New Issue
Block a user