Reputation: 781
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.
Upvotes: 2
Views: 2773
Reputation: 879
var milliseconds = 1000000000000;
var dateStr = new Date(milliseconds);
var humanreadableStr = dateStr.getDay() +'Days '+dateStr.getHours() +'Hours '+dateStr.getMinutes() +'Minutes '+dateStr.getSeconds() +'Seconds';
Upvotes: 0
Reputation: 5244
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 ) );
Upvotes: 2