根据固定的时间,获取到当前时间的月份的时间数组
比如从2018年10月获取到当前的时间的月份数组
/** * 从固定的年月获取到当前时间的数组 倒序 * @param {*} start_year 固定的年份 number 必传 * @param {*} start_mon 固定的月份 number 必传 * @param {*} type 需要'-'或不需要'' string 必传 */ export function GetPreMonthDay (start_year, start_mon, type) { //创建现在的时间 let data = new Date(); //获取年 let year = data.getFullYear(); //获取月 let mon = data.getMonth() + 1; let arry = []; let num = 13; let list = []; let mon1 = mon,mon2 = mon, year1 = year, year2; for (let i = 0; i < (num - start_mon); i++) { mon1 = i == 0 ? start_mon : parseInt(mon1) + 1; if (parseInt(mon1) <= 0) { start_year = parseInt(start_year) - 1; mon1 = parseInt(mon1) + 12; } else if (parseInt(mon1) > 12) { start_year = parseInt(start_year) + 1; mon1 = parseInt(mon1) - 12; } if (parseInt(mon1) < 10) { mon1 = "0" + mon1; } arry.unshift({ name: start_year + "年" + mon1 + '月', value: start_year + type + mon1, }); } if (year1 > start_year) { for (let y = 0; y < (year1 - start_year); y++) { year2 = y == 0 ? start_year + 1 : year2 + 1; for (let i = 0; i < 12; i++) { mon2 = i == 0 ? 1 : parseInt(mon2) + 1; if (year2 == year && mon2 > mon) { break; } if (parseInt(mon2) <= 0) { year2 = parseInt(year2) - 1; mon2 = parseInt(mon2) + 12; } else if (parseInt(mon2) > 12) { year2 = parseInt(year2) + 1; mon2 = parseInt(mon2) - 12; } if (parseInt(mon2) < 10) { mon2 = "0" + mon2; } list.unshift({ name: year2 + "年" + mon2 + '月', value: year2 + type + mon2, }) } } } let time = list.concat(arry); return time; }
这个是倒序的,如果需要正序,就把unshift更改为push