Created
March 14, 2019 17:20
-
-
Save fernandocamargo/7d9c7c19e015ba9e841bfa4da4b90742 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 convert = seconds => | |
[ | |
{ label: "year(s)", amount: 31104000 }, | |
{ label: "month(s)", amount: 2592000 }, | |
{ label: "week(s)", amount: 604800 }, | |
{ label: "day(s)", amount: 86400 }, | |
{ label: "hour(s)", amount: 3600 }, | |
{ label: "minute(s)", amount: 60 }, | |
{ label: "second(s)", amount: 1 } | |
] | |
.reduce( | |
({ input, output }, { label, amount }) => { | |
const total = Math.floor(input / amount); | |
return { | |
output: !total ? output : output.concat(`${total} ${label}`), | |
input: input - total * amount | |
}; | |
}, | |
{ input: seconds, output: [] } | |
) | |
.output.join(", "); | |
console.clear(); | |
console.log(convert(50)); | |
console.log(convert(121)); | |
console.log(convert(1000)); | |
console.log(convert(3600)); | |
console.log(convert(10000)); | |
console.log(convert(604800)); | |
console.log(convert(608400)); | |
console.log(convert(100000)); | |
console.log(convert(1000000)); | |
console.log(convert(10000000)); | |
console.log(convert(100000000)); | |
console.log(convert(1000000000)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment