Created
May 20, 2017 02:04
-
-
Save erkobridee/34cd4c5a6cc42b6e9b7610dcff83152a to your computer and use it in GitHub Desktop.
output a pretty time string from a given milliseconds time
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
// value in milliseconds | |
function prettyTime(value){ | |
var days = Math.floor(value/86400000); | |
value = value%86400000; | |
var hours = Math.floor(value/3600000); | |
value = value%3600000; | |
var minutes = Math.floor(value/60000); | |
value = value%60000; | |
var seconds = Math.floor(value/1000); | |
value = value%1000; | |
return ( | |
(days? days + 'd ' : '') + | |
(hours? hours + 'h ' : '') + | |
(minutes? minutes + 'm ' : '') + | |
(seconds? seconds + 's ' : '') + | |
(value? value + 'ms' : '') | |
); | |
} | |
console.log(prettyTime(500000)); // 8m 20s | |
console.log(prettyTime(1000)); // 1s | |
var start = new Date; | |
setTimeout(function (argument) { | |
// execution time simulated with setTimeout function | |
var end = new Date() - start; | |
console.log(prettyTime(end)); | |
}, 1234); // 1s 238ms |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
node package to do the same work: