Reputation: 235
I have a JSON service providing me with minutes. I then wish to convert these minutes into a DD:HH:MM format. [Days, Hours, Minutes]
Can anyone point me in the right direction or tell me how to accomplish this?
Thanks Devin
Upvotes: 0
Views: 96
Reputation: 22570
I know you found your answer, but just throwing this out there because it seems easier to me, maybe help someone else:
function makeTime(v, lead) {
v = parseInt(v); // ensures variable is an integer to do math too
var days = Math.floor(v/1440),
hrs = Math.floor(v/60 - (days*24)),
mins = xmins % 60;
var ret = {
days: lead ? "" + (days < 10 ? "0" + days : days) : days,
hrs: lead ? "" + (hrs < 10 ? "0" + hrs : hrs) : hrs,
mins: lead ? "" + (mins < 10 ? "0" + mins : mins) : mins
};
ret.combo = ret.days + ":" + ret.hrs + ":" + ret.mins;
return ret;
}
var xmins = 11404; // ou can se your data or whatever, this is just an example of minutes
var breakDown = makeTime(xmins); // this will return pure integers on each value
// OR!!
var breakDown = makeTime(xmins, true); // the true parameter just simply decides if you want leading variable, and string on each
// use
$("#eleID").text(breakDown.combo);
Upvotes: 0
Reputation: 8322
try this
var totalMinutes = ;
var days = totalMinutes/1440;
var totalHours = totalMinutes%1440;
var hours = totalHours/60;
var minutes = totalHours%60;
alert("DD:"+days+" HH:"+hours+" MM:"+minutes);
Upvotes: 0
Reputation: 147363
You can convert minutes into days, hours and minutes using something like:
// Helper
function z(n) {
return (n<10? '0' : '') + n;
}
var n = 5467;
var days = n / 1440 | 0;
var hours = n % 1440 / 60 | 0;
var mins = n % 60;
alert( z(days) + ':' + z(hours) + ':' + z(mins) ); // 03:19:07
Upvotes: 1
Reputation: 3353
If you don't want to write your own function for this you can convert your number to milliseconds and use that to create a date object. Then output that date object using whatever format you choose to.
Upvotes: 0
Reputation: 154818
You can create a common function that removes a number as high as possible, and then continues again with the remainder: http://jsfiddle.net/sL43t/1/.
var arr = [];
var value = 5467;
var gain = function(minutes) {
var amount = Math.floor(value / minutes);
arr.push(amount < 10 ? "0" + amount : amount); // add zero padding if needed
value %= minutes;
};
gain(24 * 60); // minutes per day
gain(60); // minutes per hour
gain(1); // minutes per minute
var str = arr.join(":");
Upvotes: 1
Reputation: 10880
// assuming obj.totalMin to be total minutes
var obj = {
totalMin : 4859
}
var days = Math.floor( obj.totalMin / (60 * 24));
var hours = Math.floor( obj.totalMin / 60) - (days * 24);
var minutes = obj.totalMin % 60;
document.write (days + ":" + hours + ":" + minutes);
Upvotes: 0
Reputation: 1565
var t = 123456; // the given minutes
var days = Math.floor(t / (60 * 24));
var hours = Math.floor((t - days * 60 * 24) / 60);
var mintues = t - days * 60 * 24 - hours * 60;
Upvotes: 0