到目前为止,我已经完成了这个功能,但它看起来很冗长,很丑陋。有没有办法动态生成月份或其他方法。
为了更好地理解,请看这个小提琴 http://jsfiddle.net/Yq3DW/67/
function formatMonth(d) {
switch (d) {
case 0:
return "Jan";
break;
case 1:
return "Feb";
break;
case 2:
return "Mar";
break;
case 3:
return "Apr";
break;
case 4:
return "May";
break;
case 5:
return "Jun";
break;
case 6:
return "jul";
break;
case 7:
return "Aug";
break;
case 8:
return "Sep";
break;
case 9:
return "Oct";
break;
case 10:
return "Nov";
break;
case 11:
return "Dec";
default:
return "Jan"
}
}
function (d) {
return formatMonth(d);
}
答案 0 :(得分:2)
var formatMonth = function(i) {
return ["Jan", "Feb", "Mar", "Jul", "Jun", "Aug", "Sep", "Oct", "Nov", "Dec"][i];
};
console.log(1, formatMonth(1));
console.log(0, formatMonth(0));

Open console

答案 1 :(得分:1)
function formatMonth(d) {
var months = ["Jan", "Feb", "Mar", "Jul", "Jun", "Aug", "Sep", "Oct", "Nov", "Dec"];
return month = months[d];
}
formatMonth(3)
答案 2 :(得分:1)
如果您正好使用moment
库,那么它就是
> moment.months(0)
"January"
> momenet.monthsShort(0)
"Jan"