DEV Community

Arnaud Leymet
Arnaud Leymet

Posted on • Edited on

Display Formatted Durations in TypeScript 🗓️

const formattedDuration = (duration: number): string => {
  const sec = duration % 60
  const min = Math.floor(duration / 60)
  return `${min}min${sec > 0 ? ` ${sec}s` : ''}`
}

formattedDuration(600)
//=> "10min"

formattedDuration(640)
//=> "10min 40s"

formattedDuration(6640)
//=> "110min 40s"
Enter fullscreen mode Exit fullscreen mode

▶️ Try it out

Top comments (0)