2016-09-26 19 views
0

因此,首先這不是關於getMonth從0-11而不是1-12返回數字,並且爲我的英語感到抱歉。JavaScript函數date.getMonth()不檢索正確的月份數? (11而不是8)

我想製作一個簡單的程序來顯示瀏覽器上的當前日期,如「九月是月,二十六是一天,二零一六是一年」。我這樣做:

!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歐羅巴開賽)

8月 - 9月

(「Ora legale Europa occidentale」=我當地的時間,這裏不應該有關)。

但由於種種原因,這是我所得到的,當我運行程序:

週一九月 26 2016 23時38分39秒GMT + 0200(ORA LEGALE歐羅巴開賽)

11日 - 12月

月是在正確的日期(說「九月」),但錯在對方輸出(M =「11」,這將導致變種FM是「月」) 爲什麼? :(

+7

你所有的'如果(M = XX)'語句分配,而不是比較。應該是:'if(m === xx)' – 2016-09-26 21:54:50

+2

也就是說,'m = 0'將'm'設置爲'0'並返回'0'。然而'm == 0'(或'm === 0)'測試變量'm'是否設置爲'0' –

+0

哦,謝謝!解決 –

回答

6

您應該使用==而不是=; =正在分配和==正在比較。

但是,通過使用數組映射月份名稱,可以使代碼更優雅。

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

替換八月...較8月和其他必要的月份名稱

+0

僅靠這一點就無法解決他們的錯誤。還有一個事實,如果月份不是12月,而不是月份中的任何月份,else語句將fm設置爲Error01。 – Tobsta

3

您不斷變化的米你的代碼的價值和FM:

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

...將米到零,然後去一個,然後兩個......最後一個是運行m = 11和fm到12月,這將會一直髮生。

在你的if語句中修改= = ==以解決問題,並刪除else,如果正確使用else,或者使用開關(else爲只寫適用於LAST if子句)

請參閱關於其他部分if here

1

有兩個問題與您的代碼。首先是你分配變量而不是在你的if語句中測試它們。

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

以上是錯誤的,因爲你說m = 0,然後if語句運行,因爲m = 0被解析成m是0,這意味着是的,可變m存在,所以如果條件爲真和代碼運行。這一直持續到最後一個,它的值爲11,所以fm ='12月'。

的第二個問題是,最後一個條件,這是應該來檢查,看看是否月份是一個有效的值,如果它是不是這樣說的一個明顯的方式,實際上並沒有做到這一點。

else {fm="Error01"} 

當月份不是12月份時,else語句正在運行,因爲它只連接到12月份的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>