(dateString: string)
| 224 | * @returns A human-readable relative time string |
| 225 | */ |
| 226 | export function formatRelativeTime(dateString: string): string { |
| 227 | const date = new Date(dateString) |
| 228 | const now = new Date() |
| 229 | const diffInSeconds = Math.floor((now.getTime() - date.getTime()) / 1000) |
| 230 | |
| 231 | if (diffInSeconds < 60) { |
| 232 | return 'just now' |
| 233 | } |
| 234 | if (diffInSeconds < 3600) { |
| 235 | const minutes = Math.floor(diffInSeconds / 60) |
| 236 | return `${minutes}m ago` |
| 237 | } |
| 238 | if (diffInSeconds < 86400) { |
| 239 | const hours = Math.floor(diffInSeconds / 3600) |
| 240 | return `${hours}h ago` |
| 241 | } |
| 242 | if (diffInSeconds < 604800) { |
| 243 | const days = Math.floor(diffInSeconds / 86400) |
| 244 | return `${days}d ago` |
| 245 | } |
| 246 | if (diffInSeconds < 2592000) { |
| 247 | const weeks = Math.floor(diffInSeconds / 604800) |
| 248 | return `${weeks}w ago` |
| 249 | } |
| 250 | if (diffInSeconds < 31536000) { |
| 251 | const months = Math.floor(diffInSeconds / 2592000) |
| 252 | return `${months}mo ago` |
| 253 | } |
| 254 | const years = Math.floor(diffInSeconds / 31536000) |
| 255 | return `${years}y ago` |
| 256 | } |
no outgoing calls
no test coverage detected