JavaScript函数date.getMonth()未检索到正确的月份号?(11而不是8)

JavaScript function date.getMonth() not retrieving the correct month number? (11 instead of 8)

本文关键字:getMonth date 函数 检索 JavaScript      更新时间:2023-09-26

所以首先这不是关于getMonth返回0-11而不是1-12的数字,很抱歉我的英语。

我想制作一个简单的程序来在浏览器上显示当前日期,比如";9月是月,26日是日,2016年是年;。我做到了:

!DOCTYPE HTML>
<html>
<body>
    <p id="test"></p>               // this is where date will be displayed
    <script>
        var date = new Date();      // i get the date 
        var m = date.getMonth();    // i get the month
        var fm;                     // all this is to convert number to month name
        if (m=0){fm="January";}
        if (m=1){fm="February";}
        if (m=2){fm="March";}
        if (m=3){fm="April";}
        if (m=4){fm="May";}
        if (m=5){fm="Jun";}
        if (m=6){fm="July";}
        if (m=7){fm="August";}
        if (m=8){fm="September";}
        if (m=9){fm="October";}
        if (m=10){fm="November";}
        if (m=11){fm="December";}
        else {fm="Error01"}
        var d = date.getDate();      // the day
        var y = date.getFullYear();
        //document.getElementById("test").innerHTML= fm + " is the month, " + d + " is the Date(day) and "+ y + " is the full year"; 
        // i removed this ^ to highlight the new output below:
        document.getElementById("test").innerHTML= date + "<br>" + m + " - " + fm;
    </script>
</body>
</html>

此时输出应为:

2016年9月26日星期一23:38:39 GMT+0200(ora legale Europa occidentale(

8-9月

("Ora legale Europa occidentale"=我的当地时间,这里不应该有关系(。

但由于某些原因,这是我运行程序时得到的:

星期一九月2016年26日23:38:39 GMT+0200(ora legale Europa occidentale(

12月11日

月份在日期上是正确的(说"9月"(,但在其他输出上是错误的(m="11",这导致var fm为"12月"(为什么?:(

您应该使用==而不是=;CCD_ 3正在分配并且CCD_ 4正在比较。

但是,您可以通过使用数组映射月份名称来使代码更加优雅。

 <script>
            var date = new Date();      // i get the date 
            var m = date.getMonth();    // i get the month
            var monthMap = ["January", "Febrauary", "Mar..", "Apr..", "May", "June", "July", "Aug..", "Sept", "Oct", "Nov", "Dec"];
            var d = date.getDate();      // the day
            var y = date.getFullYear();
            //document.getElementById("test").innerHTML= monthMap[m] + " is the month, " + d + " is the Date(day) and "+ y + " is the full year"; 
            // i removed this ^ to highlight the new output below:
            document.getElementById("test").innerHTML= date + "<br>" + m + " - " + monthMap[m];
        </script>

将August..替换为August和其他必要的月份名称

您不断更改代码中m和fm的值:

if (m=0){fm="January";}
...
if (m=11){fm="December";}

将m设置为零,然后设置为一,然后设置成二。。。最后一次运行是m=11,fm到12月,这将永远发生。

将if语句中的=更改为==以进行修复。并删除else,如果正确则使用else,或者使用开关(所写的else仅适用于LAST if子句(。

请参阅其他部分,如果此处为

您的代码有两个问题。第一种方法是在if语句中分配变量,而不是测试它们。

if(m=0){fm="January"}

上面的内容是错误的,因为您说的是m = 0,然后运行if语句,因为m = 0被解析为m,即0,这意味着是的,变量m存在,所以if条件为true,代码运行。这种情况一直持续到最后一个,它的值为11,因此fm="十二月"。

第二个问题是,最后一个条件,即应该检查月份是否是有效值,如果不是,它会以明显的方式表明这一点,但实际上并没有做到。

else {fm="Error01"}

else语句在月份不是十二月时运行,因为它只连接到十二月if语句。通过在第一个else-if语句之后生成所有if语句,我们可以解决这个问题,并使原始代码正常工作。

<!DOCTYPE HTML>
<html>
<body>
    <p id="test"></p>               // this is where date will be displayed
    <script>
        var date = new Date();      // i get the date 
        var m = date.getMonth();    // i get the month
        var fm;                     // all this is to convert number to month name
        if (m===0){fm="January";}
        else if (m===1){fm="February";}
        else if (m===2){fm="March";}
        else if (m===3){fm="April";}
        else if (m===4){fm="May";}
        else if (m===5){fm="Jun";}
        else if (m===6){fm="July";}
        else if (m===7){fm="August";}
        else if (m===8){fm="September";}
        else if (m===9){fm="October";}
        else if (m===10){fm="November";}
        else if (m===11){fm="December";}
        else {fm="Error01"}console.log(fm)
        var d = date.getDate();      // the day
        var y = date.getFullYear();
        document.getElementById("test").innerHTML= fm + " is the month, " + d + " is the Date(day) and "+ y + " is the full year"; 
        // i removed this ^ to highlight the new output below:
        //document.getElementById("test").innerHTML= date + "<br>" + m + " - " + fm;
      //the above code has completed tests properly, and so has been replaced by the original code
    </script>
</body>
</html>