2017-06-28 8 views
1

私はgetMonth() JavaScriptが正しくない値を返すという問題があります。私は前の5ヶ月を見せたい。問題は現在、日付は29/06/17ですが、今年は2月28日です。だから私に間違った出力を返します。この問題を解決するアイデアはありません。誰かがこの問題を解決する方法を知っているなら、私に提案するか、教えてください。ありがとうございました。javacriptのget.month()は2月に間違った出力を返します

enter image description here

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>

+0

ちょっと使いました – Hackerman

+0

@Hackerman私はそれについて知らなかったので、試してみます。ありがとうございました:) – Nothingnez

+0

私はいつもmoment.jsを使用して素晴らしいですが、この場合は私の答えを使用する方が簡単です。 – Manatax

答えて

2

変更月の最初の日: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>

関連する問題