time.ts 761 B

12345678910111213141516171819202122232425262728
  1. import { formatDistanceToNowStrict } from 'date-fns'
  2. export const formatDateAgo = (date: Date): string => {
  3. return `${formatDistanceToNowStrict(date)} ago`
  4. }
  5. export const formatDurationShort = (duration: number): string => {
  6. const MINUTES_IN_HOUR = 60
  7. const SECONDS_IN_HOUR = MINUTES_IN_HOUR * 60
  8. const normalize = (n: number) => n.toString().padStart(2, '0')
  9. let remaining = duration
  10. const hours = Math.floor(remaining / SECONDS_IN_HOUR)
  11. remaining = remaining % SECONDS_IN_HOUR
  12. const minutes = Math.floor(remaining / MINUTES_IN_HOUR)
  13. remaining = remaining % MINUTES_IN_HOUR
  14. const seconds = remaining
  15. if (hours) {
  16. return `${hours}:${normalize(minutes)}:${normalize(seconds)}`
  17. }
  18. return `${minutes}:${normalize(seconds)}`
  19. }