Reputation: 203
I have the problem that getMonth()
fucntion in javascript returns a wrong value. I want to show the 5 months previous. The problem is now the date is 29/06/17 but for this year, February has 28 days. So it returns a wrong output to me. I have no ideas to solve this problem. If anyone know how to solve this problem please suggest me or tell me. Thank you.
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>
Upvotes: 1
Views: 380
Reputation: 4223
Change the date to the first of the month: 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>
Upvotes: 3