2

I have a duration in milliseconds that I want to turn into days, hours in minutes so the output looks like this:

"473 days 17 hours and 28 minutes"

I can't find an answer to how to do this. I would really appreciate some help.

depperm
  • 10,606
  • 4
  • 43
  • 67
Rawland Hustle
  • 781
  • 1
  • 10
  • 16
  • nothing to do with jquery. that's just basic math... – Marc B Aug 30 '16 at 17:36
  • [JavaScript: Convert milliseconds to object with days, hours, minutes, and seconds](https://gist.github.com/remino/1563878) – empiric Aug 30 '16 at 17:38
  • 3
    Possible duplicate of [Javascript show milliseconds as days:hours:mins without seconds](http://stackoverflow.com/questions/8528382/javascript-show-milliseconds-as-dayshoursmins-without-seconds) – depperm Aug 30 '16 at 17:39

2 Answers2

2

Please check below snippet. After passing milliseconds you will find result in days hours and minutes.

function dhm(t){
    var cd = 24 * 60 * 60 * 1000,
        ch = 60 * 60 * 1000,
        d = Math.floor(t / cd),
        h = Math.floor( (t - d * cd) / ch),
        m = Math.round( (t - d * cd - h * ch) / 60000),
        pad = function(n){ return n < 10 ? '0' + n : n; };
  if( m === 60 ){
    h++;
    m = 0;
  }
  if( h === 24 ){
    d++;
    h = 0;
  }
  return d +" days : "+ pad(h) +" hours : "+ pad(m) + " mins ";
}
var days = (473 * 24 * 60 * 60 * 1000);
var hours = (17 * 60 * 60 * 1000);
var mins = (28 * 60 * 1000);
var milliseconds  = days + hours + mins;
console.log( dhm( milliseconds ) );
Rahul Patel
  • 5,248
  • 2
  • 14
  • 26
0
var milliseconds = 1000000000000;
var dateStr = new Date(milliseconds);
var humanreadableStr = dateStr.getDay() +'Days '+dateStr.getHours() +'Hours '+dateStr.getMinutes() +'Minutes '+dateStr.getSeconds() +'Seconds';
empiric
  • 7,825
  • 7
  • 37
  • 48