因此,首先這不是關於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是「月」) 爲什麼? :(
你所有的'如果(M = XX)'語句分配,而不是比較。應該是:'if(m === xx)' – 2016-09-26 21:54:50
也就是說,'m = 0'將'm'設置爲'0'並返回'0'。然而'm == 0'(或'm === 0)'測試變量'm'是否設置爲'0' –
哦,謝謝!解決 –