Skip to content

Instantly share code, notes, and snippets.

@craiggoldstone
Created April 26, 2017 11:54
Show Gist options
  • Save craiggoldstone/123c61d2fe3660d5ed3113eb1e681e73 to your computer and use it in GitHub Desktop.
Save craiggoldstone/123c61d2fe3660d5ed3113eb1e681e73 to your computer and use it in GitHub Desktop.
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