Last active
October 19, 2023 15:18
-
-
Save frosas/a5c9f7403818eff7863ac3aa3d371916 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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