Skip to content

Instantly share code, notes, and snippets.

@johnjullies
Forked from remino/msconvert.js
Last active August 29, 2015 14:27
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 johnjullies/e849ca298293b5cf0d35 to your computer and use it in GitHub Desktop.
Save johnjullies/e849ca298293b5cf0d35 to your computer and use it in GitHub Desktop.
JavaScript: Convert milliseconds to object with days, hours, minutes, and seconds
function convertMS(ms) {
var y, mo, d, h, m, s;
s = Math.floor(ms / 1000);
m = Math.floor(s / 60);
s = s % 60;
h = Math.floor(m / 60);
m = m % 60;
d = Math.floor(h / 24);
h = h % 24;
mo = Math.floor(d / 30);
d = d % 30;
d = Math.floor(d / 7);
y = Math.floor(mo / 12);
mo = mo % 12;
return { y: y, mo: mo, d: d, h: h, m: m, s: s };
}
@johnjullies
Copy link
Author

Added years and months property from @remino's version.

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