1
私はgetMonth()
JavaScriptが正しくない値を返すという問題があります。私は前の5ヶ月を見せたい。問題は現在、日付は29/06/17ですが、今年は2月28日です。だから私に間違った出力を返します。この問題を解決するアイデアはありません。誰かがこの問題を解決する方法を知っているなら、私に提案するか、教えてください。ありがとうございました。javacriptのget.month()は2月に間違った出力を返します
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>
ちょっと使いました – Hackerman
@Hackerman私はそれについて知らなかったので、試してみます。ありがとうございました:) – Nothingnez
私はいつもmoment.jsを使用して素晴らしいですが、この場合は私の答えを使用する方が簡単です。 – Manatax