Skip to content

Instantly share code, notes, and snippets.

@edvaldoszy
Created January 23, 2020 13:24
Show Gist options
  • Save edvaldoszy/c7c0c1f4074f0d25bdf1a653e3c4b83d to your computer and use it in GitHub Desktop.
Save edvaldoszy/c7c0c1f4074f0d25bdf1a653e3c4b83d to your computer and use it in GitHub Desktop.
convert-time-to-human-readable
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