Created
April 26, 2017 11:54
-
-
Save craiggoldstone/123c61d2fe3660d5ed3113eb1e681e73 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
function transform(seconds) { | |
const times = { | |
year: 31557600, | |
month: 2629746, | |
day: 86400, | |
hour: 3600, | |
minute: 60, | |
second: 1 | |
}; | |
let timeString: string = ''; | |
let plural: string = ''; | |
for (let key in times) { | |
if (Math.floor(seconds / times[key]) > 0) { | |
if (Math.floor(seconds / times[key]) > 1) { | |
plural = 's'; | |
} else { | |
plural = ''; | |
} | |
timeString += Math.floor(seconds / times[key]).toString() + ' ' + key.toString() + plural + ' '; | |
seconds = seconds - times[key] * Math.floor(seconds / times[key]); | |
console.log('using ' + timeString); | |
} | |
} | |
return timeString; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment