Skip to content

Instantly share code, notes, and snippets.

@SauloSilva
Last active December 21, 2015 17:38
Show Gist options
  • Save SauloSilva/6341339 to your computer and use it in GitHub Desktop.
Save SauloSilva/6341339 to your computer and use it in GitHub Desktop.
Transform ms in days, hours, minutes and seconds.
// example 1: format "Days" : days, "Hours" : hours, "Minutes" : minutes, "Seconds" : seconds
millisToTime = function(ms) {
x = ms / 1000;
seconds = Math.round(x % 60);
x /= 60;
minutes = Math.round(x % 60);
x /= 60;
hours = Math.round(x % 24);
x /= 24;
days = Math.round(x);
return {"Days" : days, "Hours" : hours, "Minutes" : minutes, "Seconds" : seconds};
}
// example 2: format 00:00:00 => day:hour:minute
function dhm(ms){
var cDay = 24 * 60 * 60 * 1000
, cHour = 60 * 60 * 1000
, day = Math.floor(ms / cDay)
, hour = '0' + Math.floor( (ms - day * cDay) / cHour)
, minute = '0' + Math.round( (ms - day * cDay - hour * cHour) / 60000);
return [day, hour.substr(-2), minute.substr(-2)].join(':');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment