我有此功能可将分钟转换为小时/分钟格式:
return Math.floor(diffMins / 60) + " Hour " + diffMins % 60 + " Minutes.";
我想做的是,如果没有时间,那么" 0小时"不显示。任何人都可以推荐一种我能做到的好方法吗?
答案 0 :(得分:2)
地板;检查是否超过0小时;如果没有,输出空字符串;否则检查是否一小时,如果是,则输出" 1小时"如果没有,则输出小时数+"小时"
var h = Math.floor(diffMins / 60);
var hours = (h > 0 ? (h==1 ? "1 hour" : h+" hours") : "");
var mins = diffMins % 60;
return hours + mins > 1 ? mins + " minutes." : " 1 minute." ;
答案 1 :(得分:1)
这会有效..
return (diffMins>60?Math.floor(diffMins / 60) + " Hour ":"") + diffMins % 60 + " Minutes."
答案 2 :(得分:1)
function makePlural(count, words) {
if (count == 0 || count > 1)
return count + " " + words[1];
return count + " " + words[0];
}
function minutesToString(diffMins) {
var hours = Math.floor(diffMins / 60);
var minutes = diffMins % 60;
var hoursStr = (hours == 0) ? "" : (makePlural(hours, ["Hour", "Hours"]) + " ");
var minutesStr = makePlural(minutes, ["minute", "minutes"]);
return hoursStr + minutesStr;
}
答案 3 :(得分:1)
return Math.floor(diffMins / 60) ==0 ? diffMins % 60 + " Minutes." : Math.floor(diffMins / 60) + " Hour " + diffMins % 60 + " Minutes."
答案 4 :(得分:0)
function yourFunction(diffMins) {
if (minutes < 60) {
return minutes + " Minutes.";
}
return Math.floor(minutes / 60) + " Hour " + minutes % 60 + " Minutes.";
}