(date: Date)
| 458 | } |
| 459 | |
| 460 | export const getFormattedDate = (date: Date) => { |
| 461 | const now = new Date(); |
| 462 | const diffMinutes = (now.getTime() - date.getTime()) / (1000 * 60); |
| 463 | const isFuture = diffMinutes < 0; |
| 464 | |
| 465 | // Use absolute values for calculations |
| 466 | const minutes = Math.abs(diffMinutes); |
| 467 | const hours = minutes / 60; |
| 468 | const days = hours / 24; |
| 469 | const months = days / 30; |
| 470 | |
| 471 | const formatTime = (value: number, unit: 'minute' | 'hour' | 'day' | 'month', isFuture: boolean) => { |
| 472 | const roundedValue = Math.floor(value); |
| 473 | const pluralUnit = roundedValue === 1 ? unit : `${unit}s`; |
| 474 | |
| 475 | if (isFuture) { |
| 476 | return `In ${roundedValue} ${pluralUnit}`; |
| 477 | } else { |
| 478 | return `${roundedValue} ${pluralUnit} ago`; |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | if (minutes < 1) { |
| 483 | return 'just now'; |
| 484 | } else if (minutes < 60) { |
| 485 | return formatTime(minutes, 'minute', isFuture); |
| 486 | } else if (hours < 24) { |
| 487 | return formatTime(hours, 'hour', isFuture); |
| 488 | } else if (days < 30) { |
| 489 | return formatTime(days, 'day', isFuture); |
| 490 | } else { |
| 491 | return formatTime(months, 'month', isFuture); |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | /** |
| 496 | * Converts a number to a string |
no test coverage detected