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

So first of all this is not about getMonth returning numbers from 0-11 instead of 1-12, and sorry for m

So first of all this is not about getMonth returning numbers from 0-11 instead of 1-12, and sorry for my english.

I wanted to make a simple program to display the current date on the browser, like "September is the month, 26 is the day, 2016 is the year". I did this:

!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>

At This point the output should be:

Mon Sep 26 2016 23:38:39 GMT+0200 (ora legale Europa occidentale)

8 - September

("Ora legale Europa occidentale" = my local time, it shouldn't be relevant here).

But for some reasons, this is what i get when i run the program:

Mon Sep 26 2016 23:38:39 GMT+0200 (ora legale Europa occidentale)

11 - December

Month is correct in the date (says "Sept"), but wrong in the other output (m="11" which causes var fm to be "December") Why?? :(

So first of all this is not about getMonth returning numbers from 0-11 instead of 1-12, and sorry for my english.

I wanted to make a simple program to display the current date on the browser, like "September is the month, 26 is the day, 2016 is the year". I did this:

!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>

At This point the output should be:

Mon Sep 26 2016 23:38:39 GMT+0200 (ora legale Europa occidentale)

8 - September

("Ora legale Europa occidentale" = my local time, it shouldn't be relevant here).

But for some reasons, this is what i get when i run the program:

Mon Sep 26 2016 23:38:39 GMT+0200 (ora legale Europa occidentale)

11 - December

Month is correct in the date (says "Sept"), but wrong in the other output (m="11" which causes var fm to be "December") Why?? :(

Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Sep 26, 2016 at 21:52 Federico SeccoFederico Secco 531 silver badge7 bronze badges 7
  • 7 All your if(m=xx) statements are assigning instead of paring. Should be: if(m===xx) – user1106925 Commented Sep 26, 2016 at 21:54
  • 2 That is, m=0 sets m to 0 and returns that 0. However m==0 (or m === 0) tests if the variable m is set to 0 – Ruan Mendes Commented Sep 26, 2016 at 21:57
  • Ohh yea thanks! Solved – Federico Secco Commented Sep 26, 2016 at 21:57
  • 1 You need to use else if, or else the last else only applies to m==11. Also, use two equal signs. – StardustGogeta Commented Sep 26, 2016 at 22:01
  • 2 @AndrewEisenberg I would use a direct array lookup for simplicity and speed. Frederico, that code may also help you to more simply format your date/times in the future. – Phrogz Commented Sep 26, 2016 at 22:04
 |  Show 2 more ments

3 Answers 3

Reset to default 6

You should use == not =; = is assigning and == is paring.

However, you can make your code more elegant by using an array to map the month names.

 <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>

Replace Aug... with August and other necessary month names

You keep changing the value of m and fm in your code:

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

...sets m to zero, then to one, then to two... The last one to run is m=11 and fm to December, which will always happen.

Change your = to == in your if statements to fix. And remove the else, use else if correctly, or use a switch (the else as written only applies to the LAST if clause).

See the section on else if here

There are two problems with your code. The first is that you're assigning variables instead of testing them in your if statements.

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

The above is wrong because you're saying m = 0, and then the if statement runs because m = 0 is parsed into m which is 0, meaning that yes, variable m exists, so the if condition is true and the code runs. This continues until the last one, where it has the value of 11, and so fm = 'December'.

The second problem is that the last condition, which is supposed to check to see if the month is a valid value, and if it isn't it says so in an obvious way, doesn't actually do that.

else {fm="Error01"}

The else statement is running when the month isn't December, because it's only connected to the December if statement. By making all of the if statements after the first else if statements, we can fix this problem, and make your original code work.

<!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 pleted tests properly, and so has been replaced by the original code
    </script>
</body>
</html>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745185630a4615624.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信