Skip to content

Instantly share code, notes, and snippets.

@JuoCode
Last active April 16, 2016 18:07
Show Gist options
  • Save JuoCode/442e0ab53a3b27a17fe7 to your computer and use it in GitHub Desktop.
Save JuoCode/442e0ab53a3b27a17fe7 to your computer and use it in GitHub Desktop.
const DAY = 24 * 60 * 60;
const HOUR = 60 * 60;
const MINUTES = 60;
let ds, hs, ms, ss;
// day
if (duration >= DAY) {
ds = Math.floor(duration / DAY);
}
// hour
if (duration >= HOUR) {
hs = Math.floor((duration - ds * DAY) / HOUR);
}
// minutes
if (duration >= MINUTES) {
ms = Math.floor((duration - ds * DAY - hs * HOUR) / MINUTES);
}
// seconds
ss = duration % 60;
const timeUnits = [ds, hs, ms, ss];
const UNITS = [['day', 'days'], ['hour', 'hours'], ['minute', 'minutes'], ['second', 'seconds']];
const results = [];
for (let i = 0; i < timeUnits.length; i++) {
if (!timeUnits[i]) {
continue;
}
results.push(`${timeUnits[i]} ${UNITS[i][timeUnits[i] > 1 ? 1 : 0]}`);
}
return results.join(' ');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment