Skip to content

Instantly share code, notes, and snippets.

@dkubb
Created February 9, 2011 05:32
Show Gist options
  • Select an option

  • Save dkubb/817943 to your computer and use it in GitHub Desktop.

Select an option

Save dkubb/817943 to your computer and use it in GitHub Desktop.
// requires jQuery
function humanDuration(milliseconds) {
var remainder = milliseconds;
var times = [
(remainder /= 1000) % 60, // seconds
(remainder /= 60) % 60, // minutes
(remainder /= 60) % 24, // hours
(remainder /= 24), // days
].reverse();
// zero pad to 2 digits
var padded = jQuery.map(times, zeroPad(2));
// compress all leading zero digits
return padded.join(':').replace(/^(?:00:)+/, '00:');
}
function zeroPad(padTo) {
return function(number) {
var padded = Math.floor(number).toString();
while (padded.length < padTo)
padded = '0' + padded
return padded;
};
}
def human_duration(milliseconds)
remainder = milliseconds.to_i
time = [
(remainder /= 1000) % 60, # seconds
(remainder /= 60) % 60, # minutes
(remainder /= 60) % 24, # hours
(remainder /= 24), # days
].reverse
('%02d:%02d:%02d:%02d' % time).sub(/\A(?:00:)+/, '00:')
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment