javascript中的get.month()返回错误的二月输出

javascript中的get.month()返回错误的二月输出,javascript,Javascript,我的问题是javascript中的getMonth()function返回了一个错误的值。我想展示前5个月的情况。现在的问题是日期是2017年6月29日,但今年2月有28天。所以它向我返回了一个错误的输出。我没有办法解决这个问题。如果有人知道如何解决这个问题,请建议我或告诉我。多谢各位 var月=[ “一月”, “二月”, “三月”, “四月”, “五月”, “六月”, “七月”, “八月”, “9月”, “十月”, “11月”, “12月” ]; var dt=新日期(); dt.setM

我的问题是javascript中的
getMonth()
function返回了一个错误的值。我想展示前5个月的情况。现在的问题是日期是2017年6月29日,但今年2月有28天。所以它向我返回了一个错误的输出。我没有办法解决这个问题。如果有人知道如何解决这个问题,请建议我或告诉我。多谢各位

var月=[
“一月”,
“二月”,
“三月”,
“四月”,
“五月”,
“六月”,
“七月”,
“八月”,
“9月”,
“十月”,
“11月”,
“12月”
];
var dt=新日期();
dt.setMonth(dt.getMonth());
var cur_month=月[dt.getMonth()];
dt.setMonth(dt.getMonth()-1);
var pre_month_1=月[dt.getMonth()];
dt.setMonth(dt.getMonth()-1);
var pre_month_2=月[dt.getMonth()];
dt.setMonth(dt.getMonth()-1);
var pre_month_3=月[dt.getMonth()];
dt.setMonth(dt.getMonth()-1);
var pre_month_4=月[dt.getMonth()];
dt.setMonth(dt.getMonth()-1);
var pre_month_5=月[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

昨天

将日期更改为每月的第一天:
dt.setDate(1)

var月=[
“一月”,
“二月”,
“三月”,
“四月”,
“五月”,
“六月”,
“七月”,
“八月”,
“9月”,
“十月”,
“11月”,
“12月”
];
var dt=新日期();
//仅限月份定义
dt.设定日期(1);
dt.setMonth(dt.getMonth());
var cur_month=月[dt.getMonth()];
dt.setMonth(dt.getMonth()-1);
var pre_month_1=月[dt.getMonth()];
dt.setMonth(dt.getMonth()-1);
var pre_month_2=月[dt.getMonth()];
dt.setMonth(dt.getMonth()-1);
var pre_month_3=月[dt.getMonth()];
dt.setMonth(dt.getMonth()-1);
var pre_month_4=月[dt.getMonth()];
dt.setMonth(dt.getMonth()-1);
var pre_month_5=月[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

昨天

只需使用momentjs@Hackerman我不知道,我会试试看。谢谢:)我一直使用moment.js,非常棒,但是在这种情况下使用我的答案更容易。