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