Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript日期,超过12个月_Javascript_Date - Fatal编程技术网

javascript日期,超过12个月

javascript日期,超过12个月,javascript,date,Javascript,Date,我想得到过去12个月的连续12个月图表 这就是我正在做的 function formatDate(date) { date = new Date(date); var day = date.getDate(); var monthIndex = date.getMonth(); var year = date.getFullYear(); return day + ' ' + monthIndex + ' ' + year; } let now =

我想得到过去12个月的连续12个月图表

这就是我正在做的

function formatDate(date) {

    date = new Date(date);
    var day = date.getDate();
    var monthIndex = date.getMonth();
    var year = date.getFullYear();

    return day + ' ' + monthIndex + ' ' + year;
}

let now = new Date();

for ( let i=12; i>0; i--) {
    let newdate = now.setMonth(now.getMonth() - i);
    console.log(formatDate(newdate ));
}
这就是我得到的:(完全不是我所期望的。)

我想要什么

19 2 2018
19 3 2018
19 4 2018
19 5 2018
19 6 2018
19 7 2018
19 8 2018
19 9 2018
19 10 2018
19 11 2018
19 0 2019
19 1 2019
改变

let newdate = now.setMonth(now.getMonth() - i);

在您的原始代码中很容易看到月份值减少了1、2、3等等。在每次迭代中,您只需要减去1。

更改

let newdate = now.setMonth(now.getMonth() - i);


在您的原始代码中很容易看到月份值减少了1、2、3等等。在每次迭代中,您只需要减去1。

setMonth
修改日期,它不会返回修改后的副本。因此,当您第一次调用
setMonth
时,您将日期移回12个月。下一次你要把它向后移动11个月,总共23个月。然后你把它向后移动10个月,总共33个月

与其将日期连续向后移动12-i,不如一次向后移动1个月:

now.setMonth(now.getMonth() - 1);

setMonth
修改日期,但不返回修改后的副本。因此,当您第一次调用
setMonth
时,您将日期移回12个月。下一次你要把它向后移动11个月,总共23个月。然后你把它向后移动10个月,总共33个月

与其将日期连续向后移动12-i,不如一次向后移动1个月:

now.setMonth(now.getMonth() - 1);
改变这个

 now.getMonth() - i
对此

now.getMonth() - 1
函数格式日期(日期){
日期=新日期(日期);
var day=date.getDate();
var monthIndex=date.getMonth();
var year=date.getFullYear();
返回日+''+月日+''+年;
}
让我们现在=新日期();
对于(设i=12;i>0;i--){
让newdate=now.setMonth(now.getMonth()-1);
console.log(formattate(newdate));
}
更改此选项

 now.getMonth() - i
对此

now.getMonth() - 1
函数格式日期(日期){
日期=新日期(日期);
var day=date.getDate();
var monthIndex=date.getMonth();
var year=date.getFullYear();
返回日+''+月日+''+年;
}
让我们现在=新日期();
对于(设i=12;i>0;i--){
让newdate=now.setMonth(now.getMonth()-1);
console.log(formattate(newdate));

}
您只想减去1个月,所以不要使用
i
因为它是动态值。第一个循环不需要进行减法,因此请按如下所示执行

更新:您需要在循环中移动当前日期声明,因为setMonth将更改您的日期值,所以每次循环时都需要调用当前日期

函数格式日期(日期){
日期=新日期(日期);
var day=date.getDate();
var monthIndex=date.getMonth();
var year=date.getFullYear();
返回日+''+月日+''+年;
}
对于(设i=11;i>=0;i--){
让我们现在=新日期();
让newdate=now.setMonth(now.getMonth()-i);
console.log(formattate(newdate));

}
您只想减去1个月,所以不要使用
i
因为它是动态值。第一个循环不需要进行减法,因此请按如下所示执行

更新:您需要在循环中移动当前日期声明,因为setMonth将更改您的日期值,所以每次循环时都需要调用当前日期

函数格式日期(日期){
日期=新日期(日期);
var day=date.getDate();
var monthIndex=date.getMonth();
var year=date.getFullYear();
返回日+''+月日+''+年;
}
对于(设i=11;i>=0;i--){
让我们现在=新日期();
让newdate=now.setMonth(now.getMonth()-i);
console.log(formattate(newdate));

}
好的-这只是循环12次-每次减去1-因此它向后计数,而不是从-12向前计数…这就是我想要的。有什么建议吗?好吧-这只是循环12次-每次减去1-所以它倒计时,而不是从-12向前计数…这就是我想要的。有什么建议吗?好吧-这只是循环12次-每次减去1-所以它倒计时,而不是从-12向前计数…这就是我想要的。有什么建议吗?好吧-这只是循环12次-每次减去1-所以它倒计时,而不是从-12向前计数…这就是我想要的。有什么建议吗?“setMonth修改日期,它不返回修改后的副本…”,但它返回修改后的时间值。;-)“setMonth修改日期,它不会返回修改后的副本…”但它会返回修改后的时间值。;-)