format.ts 675 B

1234567891011121314151617181920
  1. export function formatDuration(secs: number) {
  2. if (secs <= 0) return ""
  3. if (secs < 60) return `${secs}s`
  4. if (secs < 3600) {
  5. const mins = Math.floor(secs / 60)
  6. const remaining = secs % 60
  7. return remaining > 0 ? `${mins}m ${remaining}s` : `${mins}m`
  8. }
  9. if (secs < 86400) {
  10. const hours = Math.floor(secs / 3600)
  11. const remaining = Math.floor((secs % 3600) / 60)
  12. return remaining > 0 ? `${hours}h ${remaining}m` : `${hours}h`
  13. }
  14. if (secs < 604800) {
  15. const days = Math.floor(secs / 86400)
  16. return days === 1 ? "~1 day" : `~${days} days`
  17. }
  18. const weeks = Math.floor(secs / 604800)
  19. return weeks === 1 ? "~1 week" : `~${weeks} weeks`
  20. }