我有一个for
循环来遍历数组并显示日期。我编写了一些代码来将月份从数字转换为文本。代码如下所示:
for (var i=0; i < arrayLength; i++) {
//==== MONTH DATA
var month = data[1];
//==== DECLARE MONTH
if (month == '01') { var month = "Jan" }
if (month == '02') { var month = "Feb" }
if (month == '03') { var month = "Mar" }
if (month == '04') { var month = "Apr" }
if (month == '05') { var month = "May" }
if (month == '06') { var month = "June" }
if (month == '07') { var month = "July" }
if (month == '08') { var month = "Aug" }
if (month == '09') { var month = "Sept" }
if (month == '10') { var month = "Oct" }
if (month == '11') { var month = "Nov" }
if (month == '12') { var month = "Dec" }
alert(month);
}
现在我想在整个网站的各种 for 循环中使用月份转换器。所以我想做一些事情,比如把它放在一个我每次都可以触发的函数中。例如:
function declareMonth(){
//==== DECLARE MONTH
if (month == '01') { var month = "Jan" }
if (month == '02') { var month = "Feb" }
if (month == '03') { var month = "Mar" }
if (month == '04') { var month = "Apr" }
if (month == '05') { var month = "May" }
if (month == '06') { var month = "June" }
if (month == '07') { var month = "July" }
if (month == '08') { var month = "Aug" }
if (month == '09') { var month = "Sept" }
if (month == '10') { var month = "Oct" }
if (month == '11') { var month = "Nov" }
if (month == '12') { var month = "Dec" }
}
for (var i=0; i < arrayLength; i++) {
//==== MONTH DATA
var month = data[1];
declareMonth();
alert(month);
}
然而,这不起作用。谁能解释如何做到这一点?