Created
January 23, 2020 13:24
-
-
Save edvaldoszy/c7c0c1f4074f0d25bdf1a653e3c4b83d to your computer and use it in GitHub Desktop.
convert-time-to-human-readable
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
function convertTimeToHumanReadable(time) { | |
const units = [ | |
['days', 480], // 8 horas (8 * 60) | |
['hours', 60], | |
['minutes', 1], | |
]; | |
function reducer({ duration, ...others }, [unit, amount]) { | |
const result = Math.floor(duration / amount); | |
const rest = duration % amount; | |
return { | |
...others, | |
duration: rest, | |
[unit]: result, | |
}; | |
} | |
return units.reduce(reducer, { duration: time }); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment