parent
476557db80
commit
44949709d1
6 changed files with 82 additions and 20 deletions
@ -0,0 +1,30 @@ |
|||||||
|
export function getRelativeTimeString(date: string): string { |
||||||
|
if (!Intl?.RelativeTimeFormat) { |
||||||
|
return date; |
||||||
|
} |
||||||
|
|
||||||
|
const rtf = new Intl.RelativeTimeFormat('en', { |
||||||
|
numeric: 'auto', |
||||||
|
style: 'narrow', |
||||||
|
}); |
||||||
|
|
||||||
|
const currentDate = new Date(); |
||||||
|
const targetDate = new Date(date); |
||||||
|
const diffInMilliseconds = currentDate.getTime() - targetDate.getTime(); |
||||||
|
|
||||||
|
const diffInMinutes = Math.round(diffInMilliseconds / (1000 * 60)); |
||||||
|
const diffInHours = Math.round(diffInMilliseconds / (1000 * 60 * 60)); |
||||||
|
const diffInDays = Math.round(diffInMilliseconds / (1000 * 60 * 60 * 24)); |
||||||
|
|
||||||
|
let relativeTime; |
||||||
|
|
||||||
|
if (diffInMinutes < 60) { |
||||||
|
relativeTime = rtf.format(-diffInMinutes, 'minute'); |
||||||
|
} else if (diffInHours < 24) { |
||||||
|
relativeTime = rtf.format(-diffInHours, 'hour'); |
||||||
|
} else { |
||||||
|
relativeTime = rtf.format(-diffInDays, 'day'); |
||||||
|
} |
||||||
|
|
||||||
|
return relativeTime; |
||||||
|
} |
Loading…
Reference in new issue