Skip to content

Instantly share code, notes, and snippets.

@erkobridee
Created May 20, 2017 02:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erkobridee/34cd4c5a6cc42b6e9b7610dcff83152a to your computer and use it in GitHub Desktop.
Save erkobridee/34cd4c5a6cc42b6e9b7610dcff83152a to your computer and use it in GitHub Desktop.
output a pretty time string from a given milliseconds time
// 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
@erkobridee
Copy link
Author

node package to do the same work:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment