Home JS获取当前月的前12个月
Post
Cancel

JS获取当前月的前12个月

1
2
3
4
5
6
7
8
9
10
11
var dataArr = [];
var data = new Date();
var year = data.getFullYear();
data.setMonth(data.getMonth()+1, 1) //获取到当前月份,设置月份
for (var i = 0; i < 12; i++) {
    data.setMonth(data.getMonth() - 1); //每次循环一次月份值减1
    var m = data.getMonth() + 1;
    m = m < 10 ? "0" + m : m;
    dataArr.push(data.getFullYear() + "-" + (m))
}
console.log(dataArr)

运行结果

This post is licensed under CC BY 4.0 by the author.