Skip to content

Commit 3d76cce

Browse files
authored
Merge pull request #151 from csr4422/enhancement/relative-time-months
Add month support to getRelativeTime
2 parents 02e8a43 + 4b8f301 commit 3d76cce

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

frontend/src/utils/datetime.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
1-
import { format, differenceInMinutes, differenceInHours, differenceInDays, differenceInYears } from 'date-fns'
1+
import { format, differenceInMinutes, differenceInHours, differenceInDays, differenceInMonths, differenceInYears } from 'date-fns'
22

33
export function getRelativeTime (timestamp, now = new Date()) {
44
try {
55
const mins = differenceInMinutes(now, timestamp)
66
const hours = differenceInHours(now, timestamp)
77
const days = differenceInDays(now, timestamp)
8+
const months = differenceInMonths(now, timestamp)
89
const years = differenceInYears(now, timestamp)
910

1011
if (mins === 0) return 'now'
1112
if (mins < 60) return `${mins}m`
1213
if (hours < 24) return `${hours}h`
13-
if (days < 365) return `${days}d`
14+
if (days < 31) return `${days}d`
15+
if (months < 12) return `${months}mo`
1416
return `${years}y`
1517
} catch (error) {
1618
console.error('Error parsing time', error, 'timestamp', timestamp)

0 commit comments

Comments
 (0)