(timestamp: string, locale = 'en-US')
| 45 | * Uses user's local timezone. |
| 46 | */ |
| 47 | export function formatRelativeTime(timestamp: string, locale = 'en-US'): string { |
| 48 | try { |
| 49 | const date = parseTimestamp(timestamp) |
| 50 | if (isNaN(date.getTime())) return '' |
| 51 | |
| 52 | const rtf = getRelativeTimeFormatter(locale) |
| 53 | const diffMs = date.getTime() - Date.now() |
| 54 | const absDiffMs = Math.abs(diffMs) |
| 55 | |
| 56 | if (absDiffMs < 30 * 1000) { |
| 57 | return rtf.format(0, 'second') |
| 58 | } |
| 59 | if (absDiffMs < HOUR_MS) return rtf.format(Math.round(diffMs / MINUTE_MS), 'minute') |
| 60 | if (absDiffMs < DAY_MS) return rtf.format(Math.round(diffMs / HOUR_MS), 'hour') |
| 61 | if (absDiffMs < WEEK_MS) return rtf.format(Math.round(diffMs / DAY_MS), 'day') |
| 62 | if (absDiffMs < MONTH_MS) return rtf.format(Math.round(diffMs / WEEK_MS), 'week') |
| 63 | if (absDiffMs < YEAR_MS) return rtf.format(Math.round(diffMs / MONTH_MS), 'month') |
| 64 | return rtf.format(Math.round(diffMs / YEAR_MS), 'year') |
| 65 | } catch { |
| 66 | return '' |
| 67 | } |
| 68 | } |
no test coverage detected
searching dependent graphs…