Skip to content

Instantly share code, notes, and snippets.

@remino
Created January 5, 2012 05:37
Show Gist options
  • Save remino/1563878 to your computer and use it in GitHub Desktop.
Save remino/1563878 to your computer and use it in GitHub Desktop.
JavaScript: Convert milliseconds to object with days, hours, minutes, and seconds
function convertMS(ms) {
var 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;
return { d: d, h: h, m: m, s: s };
};
@remino
Copy link
Author

remino commented Jan 5, 2012

You may also want to try the DateDiff and DateMeasure objects: https://gist.github.com/1563963

@shanejdonnelly
Copy link

Thank you - nice and clean, just what I needed!

@remino
Copy link
Author

remino commented Jul 30, 2015

My code for msconvert.js above is licensed under Unlicense: http://choosealicense.com/licenses/unlicense/

In simpler words, just copy it and go crazy. You don't even need to mention me.

@ZatsuneNoMokou
Copy link

Adding the rest of the ms could be nice. Something like new_ms = Math.floor((ms % 1000) * 1000) / 1000; and adding it to the return.

@juslintek
Copy link

When I do programming, most the time I get stuck with basic maths lol. 👍 And you solved my problem with mod, thats what I needed. 😈

@samueleaton
Copy link

samueleaton commented May 20, 2018

including the remaining MS:

function destructMS(milli) {
  if (isNaN(milli) || milli < 0) {
    return null;
  }

  var d, h, m, s, ms;
  s = Math.floor(milli / 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;
  ms = Math.floor((milli % 1000) * 1000) / 1000;
  return { d: d, h: h, m: m, s: s, ms: ms };
}
destructMS(10034)
//=> { d: 0, h: 0, m: 0, s: 10, ms: 34 }

@BlagovestGerov
Copy link

Thanks, I used it - msconvert.js.

@ferolika
Copy link

ferolika commented Jul 3, 2018

var timer;

var compareDate = new Date();
compareDate.setDate(compareDate.getDate() + 7); //just for this demo today + 7 days

timer = setInterval(function() {
timeBetweenDates(compareDate);
}, 1000);

function timeBetweenDates(toDate) {
var dateEntered = toDate;
var now = new Date();
var difference = dateEntered.getTime() - now.getTime();

if (difference <= 0) {

// Timer done
clearInterval(timer);

} else {

var seconds = Math.floor(difference / 1000);
var minutes = Math.floor(seconds / 60);
var hours = Math.floor(minutes / 60);
var days = Math.floor(hours / 24);

hours %= 24;
minutes %= 60;
seconds %= 60;

$("#days").text(days);
$("#hours").text(hours);
$("#minutes").text(minutes);
$("#seconds").text(seconds);

}
}

@ferolika
Copy link

ferolika commented Jul 3, 2018

var timer;

var compareDate = new Date();
compareDate.setDate(compareDate.getDate() + 7); //just for this demo today + 7 days

timer = setInterval(function() {
timeBetweenDates(compareDate);
}, 1000);

function timeBetweenDates(toDate) {
var dateEntered = toDate;
var now = new Date();
var difference = dateEntered.getTime() - now.getTime();

if (difference <= 0) {

// Timer done
clearInterval(timer);

} else {

var seconds = Math.floor(difference / 1000);
var minutes = Math.floor(seconds / 60);
var hours = Math.floor(minutes / 60);
var days = Math.floor(hours / 24);

hours %= 24;
minutes %= 60;
seconds %= 60;

$("#days").text(days);
$("#hours").text(hours);
$("#minutes").text(minutes);
$("#seconds").text(seconds);

}
}

(i want this code in typescript,will u please give the code....

@arsenal131
Copy link

arsenal131 commented Oct 30, 2019

I put code above on my little hobby project https://github.com/arsenal131/stopwatch

@gabrielbissey
Copy link

Thank you!

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