簡體   English   中英

JavaScript中的get.month()返回錯誤的輸出,與February

[英]get.month() in javascript returns wrong output with February

我有一個問題,就是javascript中的getMonth()功能返回錯誤的值。 我想展示5個月前的情況。 現在的問題是日期是17/06/17,但是今年二月有28天。 因此它向我返回了錯誤的輸出。 我沒有解決這個問題的想法。 如果有人知道如何解決此問題,請建議我或告訴我。 謝謝。

在此處輸入圖片說明

 var month = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ]; var dt = new Date(); dt.setMonth(dt.getMonth()); var cur_month = month[dt.getMonth()]; dt.setMonth(dt.getMonth() - 1); var pre_month_1 = month[dt.getMonth()]; dt.setMonth(dt.getMonth() - 1); var pre_month_2 = month[dt.getMonth()]; dt.setMonth(dt.getMonth() - 1); var pre_month_3 = month[dt.getMonth()]; dt.setMonth(dt.getMonth() - 1); var pre_month_4 = month[dt.getMonth()]; dt.setMonth(dt.getMonth() - 1); var pre_month_5 = month[dt.getMonth()]; document.getElementById("cur_month").innerHTML = cur_month; document.getElementById("pre_month_1").innerHTML = pre_month_1; document.getElementById("pre_month_2").innerHTML = pre_month_2; document.getElementById("pre_month_3").innerHTML = pre_month_3; document.getElementById("pre_month_4").innerHTML = pre_month_4; document.getElementById("pre_month_5").innerHTML = pre_month_5; 
 <select> <option id="yesterday" value="yesterday">Yesterday</option> <option id="cur_month" value="cur_month"></option> <option id="pre_month_1" value="pre_month_1"></option> <option id="pre_month_2" value="pre_month_2"></option> <option id="pre_month_3" value="pre_month_3"></option> <option id="pre_month_4" value="pre_month_4"></option> <option id="pre_month_5" value="pre_month_5"></option> </select> 

將日期更改為每月的第一天: dt.setDate(1);

 var month = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ]; var dt = new Date(); // for month defining only dt.setDate(1); dt.setMonth(dt.getMonth()); var cur_month = month[dt.getMonth()]; dt.setMonth(dt.getMonth() - 1); var pre_month_1 = month[dt.getMonth()]; dt.setMonth(dt.getMonth() - 1); var pre_month_2 = month[dt.getMonth()]; dt.setMonth(dt.getMonth() - 1); var pre_month_3 = month[dt.getMonth()]; dt.setMonth(dt.getMonth() - 1); var pre_month_4 = month[dt.getMonth()]; dt.setMonth(dt.getMonth() - 1); var pre_month_5 = month[dt.getMonth()]; document.getElementById("cur_month").innerHTML = cur_month; document.getElementById("pre_month_1").innerHTML = pre_month_1; document.getElementById("pre_month_2").innerHTML = pre_month_2; document.getElementById("pre_month_3").innerHTML = pre_month_3; document.getElementById("pre_month_4").innerHTML = pre_month_4; document.getElementById("pre_month_5").innerHTML = pre_month_5; 
 <select> <option id="yesterday" value="yesterday">Yesterday</option> <option id="cur_month" value="cur_month"></option> <option id="pre_month_1" value="pre_month_1"></option> <option id="pre_month_2" value="pre_month_2"></option> <option id="pre_month_3" value="pre_month_3"></option> <option id="pre_month_4" value="pre_month_4"></option> <option id="pre_month_5" value="pre_month_5"></option> </select> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM