上个月显示未定义

时间:2014-01-24 17:30:48

标签: javascript

function calculateTime() {
    var Make_It_12_Hour = true;
    var currentTime = new Date();
    var hour1 = currentTime.getHours() - 1; 
    var hour2 = currentTime.getHours();
    var hour3 = currentTime.getHours() + 1;
    var minutes = currentTime.getMinutes();
    var minute1 = currentTime.getMinutes() - 1;
    var minute2 = currentTime.getMinutes();
    var minute3 = currentTime.getMinutes() + 1;

    if (Make_It_12_Hour) {
        hour1 = hour1 % 12;
        hour1 = (hour1) ? hour1 : 12;
        hour2 = hour2 % 12;
        hour2 = (hour2) ? hour2 : 12;
        hour3 = hour3 % 12;
        hour3 = (hour3) ? hour3 : 12;

        if (hour1 < 0)  hour1 = "11";
        if (hour3 > 12) hour3 = "1";
        if (hour1 < 10) hour1 = "0" + hour1;
        if (hour2 < 10) hour2 = "0" + hour2;
        if (hour3 < 10) hour3 = "0" + hour3;
        if (minute1 < 0) minute1 = "59";
        if (minute3 > 59) minute3 = "1";
        if (minute1 < 10) minute1 = "0" + minute1;
        if (minute2 < 10) minute2 = "0" + minute2;  
        if (minute3 < 10) minute3 = "0" + minute3;
    } else {
        if (minute1 < 0) minute1 = "59";
        if (minute3 > 59) minute3 = "1";
        if (minute1 < 10) minute1 = "0" + minute1;
        if (minute2 < 10) minute2 = "0" + minute2;
        if (minute3 < 10) minute3 = "0" + minute3;
        if (hour1 < 0) hour1 = "23";
        if (hour3 > 23) hour3 = "0";
        if (hour1 < 10) hour1 = "0" + hour1;
        if (hour2 < 10) hour2 = "0" + hour2;
        if (hour3 < 10) hour3 = "0" + hour3;  
    }

    document.getElementById("time1-1").innerText = hour1;
    document.getElementById("time1-2").innerText = hour2;
    document.getElementById("time1-3").innerText = hour3;
    document.getElementById("time2-1").innerText = minute1;
    document.getElementById("time2-2").innerText = minute2;
    document.getElementById("time2-3").innerText = minute3;
}

function calculateDate() {

    var months = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"); 
    var currentTime = new Date();
    var day1 = (currentTime.getDate() - 1).toString();
    var day2 = currentTime.getDate().toString();
    var day3 = (currentTime.getDate() + 1).toString();
    var month = currentTime.getMonth();

    if (day1.length == 1) day1 = "0" + day1;
    if (day2.length == 1) day2 = "0" + day2;
    if (day3.length == 1) day3 = "0" + day3;

    document.getElementById("date1-1").innerText = months[month - 1];
    document.getElementById("date1-2").innerText = months[month];
    document.getElementById("date1-3").innerText = months[month + 1];
    document.getElementById("date2-1").innerText = day1;
    document.getElementById("date2-2").innerText = day2;
    document.getElementById("date2-3").innerText = day3;
}

calculateTime();
calculateDate();
setInterval(calculateTime, 2000);
setInterval(calculateDate, 60000);

您好上面列出的代码正常工作。月份显示当前和下个月但是此行输出undefined:

 document.getElementById("date1-1").innerText = months[month - 1];

我不确定是什么原因造成的。如果我将行从-1修改为+1,则显示当前月份。

jsfiddle中的完整代码。

1 个答案:

答案 0 :(得分:1)

您可以像这样获得当前月份:

var month = currentTime.getMonth();

这是0,因为JavaScript中的月份是0,我们是1月份。

然后尝试访问索引months处的数组month - 1,即-1。 months[-1]undefined

您可以使用模运算符来避免此行为(基于this other SO question的修改)

months[(((month - 1) % 12) + 12) % 12]

或者,如果您在其他问题中添加到Number原型

months[(month - 1).mod(12)]

例如,使用month = 0,我们获得months[11]