正序获取近6个月年月
const getMonth = () => {
let last6Months = []
let today : any = new Date()
let year = today.getFullYear()
for (var i = 5; i >= 0; i--) {
if ((today.getMonth() - 5 + i) < 0) {
year = Number(year) - 1
}
last6Months[5 - i] = year + "年" + fillZero(Number(today.getMonth()) + 1, 5 - i) + "月" //这里+1是因为js 的月份是0-11
}
return last6Months
}
//补零操作,保证月份为1位的时候补充前置0
const fillZero = (num : number, index : number) => {
let month = ""
if (num - index > 0 && num - index < 10) {
month = "0" + (num - index)
} else if (num - index <= 0) {
month = 12 + num - index + ""
} else if (num - index >= 10) {
month = num - index + ""
}
return month
}