Created
November 26, 2019 12:21
-
-
Save mholtzhausen/b8c9689c573c946adc8adcaa48669a3b to your computer and use it in GitHub Desktop.
Milliseconds in Human friendly output
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 humanMs = function(ms) { | |
let s = Math.floor(ms / 1000) | |
let m = Math.floor(s / 60) | |
let h = Math.floor(m / 60) | |
let d = Math.floor(h / 24) | |
ms = ms - s * 1000 | |
s = s - m * 60 | |
m = m - h * 60 | |
h = h - d * 24 | |
return `${d ? `${d}d, ` : ''}${h ? `${h}h ` : ''}${m ? `${m}m ` : ''}${ | |
s > 0 ? `${s}` : '' | |
}${ms ? `.${ms}s` : `${s ? 's' : ''}`}` | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment