我有一个 JSON 服务为我提供分钟。然后我希望将这些分钟转换为 DD:HH:MM 格式。[天,小时,分钟]
谁能指出我正确的方向或告诉我如何做到这一点?
谢谢德文
我有一个 JSON 服务为我提供分钟。然后我希望将这些分钟转换为 DD:HH:MM 格式。[天,小时,分钟]
谁能指出我正确的方向或告诉我如何做到这一点?
谢谢德文
您可以创建一个通用函数来删除尽可能高的数字,然后再继续处理剩余的数字: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(":");
您可以使用以下方法将分钟转换为天、小时和分钟:
// 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
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;
// 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);
如果您不想为此编写自己的函数,您可以将数字转换为毫秒并使用它来创建日期对象。然后使用您选择的任何格式输出该日期对象。
试试这个
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);
我知道你找到了答案,但只是把它扔在那里,因为这对我来说似乎更容易,也许可以帮助别人:
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);