Skip to content

Instantly share code, notes, and snippets.

@johan
Last active October 4, 2021 07:28
Embed
What would you like to do?
const units = [["ms", 1000], ["s", 60], ["m", 60], ["h", 24], ["d", 7], ["w", 52.142857142858986], ["y", Infinity]];
const duration = (ms) => units.reduce(
({ left, s }, [u, o]) => {
const n = left % o;
left -= n;
n && s.unshift(`${~~n}${u}`);
return { left: ~~(left / o), s };
},
{ left: ms, s: [] }
).s.join(" ");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment