Skip to content

Instantly share code, notes, and snippets.

@johan
Last active October 4, 2021 07:28
Show Gist options
  • Save johan/0b49fbbb8f6fc61efa84c289ef960bbc to your computer and use it in GitHub Desktop.
Save johan/0b49fbbb8f6fc61efa84c289ef960bbc to your computer and use it in GitHub Desktop.
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