| 170 | * Format a timestamp relative to now (e.g., "2 hours ago", "yesterday") |
| 171 | */ |
| 172 | export function formatRelativeTime(date: Date): string { |
| 173 | const now = new Date() |
| 174 | const diffMs = now.getTime() - date.getTime() |
| 175 | const diffSecs = Math.floor(diffMs / 1000) |
| 176 | const diffMins = Math.floor(diffSecs / 60) |
| 177 | const diffHours = Math.floor(diffMins / 60) |
| 178 | const diffDays = Math.floor(diffHours / 24) |
| 179 | |
| 180 | if (diffSecs < 60) { |
| 181 | return 'just now' |
| 182 | } else if (diffMins < 60) { |
| 183 | return `${diffMins}m ago` |
| 184 | } else if (diffHours < 24) { |
| 185 | return `${diffHours}h ago` |
| 186 | } else if (diffDays === 1) { |
| 187 | return 'yesterday' |
| 188 | } else if (diffDays < 7) { |
| 189 | return `${diffDays}d ago` |
| 190 | } else { |
| 191 | return date.toLocaleDateString() |
| 192 | } |
| 193 | } |