Skip to content

Instantly share code, notes, and snippets.

@HonbraDev
Created December 28, 2020 20:37
Show Gist options
  • Save HonbraDev/5964800e9da6ddb005c47b7b100cc166 to your computer and use it in GitHub Desktop.
Save HonbraDev/5964800e9da6ddb005c47b7b100cc166 to your computer and use it in GitHub Desktop.
JavaScript - Go from a time in ms to human-readable
//go from a time in ms to human-readable
function formatTime(t){
var hrs = Math.floor(t / (1000*60*60));
t = t % (1000*60*60);
var mins = Math.floor(t / (1000*60));
t = t % (1000*60);
var secs = Math.floor(t / 1000);
t = padZeros((t % 1000).toString(), 3).substring(0, 2);
return padZeros(mins, 2) + ":" + padZeros(secs, 2) + '.' + t;
}
function padZeros(n, len){
var s = n.toString();
while (s.length < len){
s = '0' + s;
}
return s;
}
// copied from https://github.com/duckduckgo/zeroclickinfo-goodies/blob/5521881a99e0e912e076cfc50016bfb81a0bcc17/share/goodie/stopwatch/stopwatch.js#L27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment