Last active
April 16, 2016 18:07
-
-
Save JuoCode/442e0ab53a3b27a17fe7 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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