Skip to content

Instantly share code, notes, and snippets.

@frosas
Last active October 19, 2023 15:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save frosas/a5c9f7403818eff7863ac3aa3d371916 to your computer and use it in GitHub Desktop.
Save frosas/a5c9f7403818eff7863ac3aa3d371916 to your computer and use it in GitHub Desktop.
const SECONDS = 1000
const MINUTES = 60 * SECONDS
const HOURS = 60 * MINUTES
const DAYS = 24 * HOURS
function convertTime(duration: number) {
const [days, daysReminder] = integerDivide(duration, DAYS)
const [hours, hoursReminder] = integerDivide(daysReminder, HOURS)
const [minutes, minutesReminder] = integerDivide(hoursReminder, MINUTES)
const [seconds, milliseconds] = integerDivide(minutesReminder, SECONDS)
return { days, hours, minutes, seconds, milliseconds }
}
function integerDivide(numerator: number, denominator: number) {
const quotient = Math.floor(numerator / denominator)
const remainder = numerator % denominator
return [quotient, remainder]
}
console.log(convertTime(206 * MINUTES))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment