分别传入开始年份(start)和结束年份(end),每一时间级都存在一个二维数组里,最后返回的是一个6维数组。
1 function genCalendar(start, end) { 2 var calendar = []; 3 start = start || 1970; 4 end = end || new Date().getFullYear(); 5 /* 循环年 */ 6 for (var y = start; y <= end; y++) { 7 var yArr = []; 8 yArr[2] = []; 9 yArr[0] = y.toString(); 10 yArr[1] = y + "年"; 11 12 /* 循环月 */ 13 for (var m = 1; m <= 12; m++) { 14 var mArr = []; 15 mArr[2] = []; 16 mArr[0] = m.toString(); 17 mArr[1] = m + "月"; 18 19 /* 循环日 */ 20 var days = new Date(y, m, 0).getDate(); //获得当月总天数 21 for (var d = 1; d <= days; d++) { 22 var dArr = []; 23 dArr[0] = d.toString(); 24 dArr[1] = d + "日"; 25 mArr[2].push(dArr); 26 } // deep3 27 yArr[2].push(mArr); 28 } // deep2 29 calendar.push(yArr); 30 } // deep1 31 //console.log(calendar); 32 return calendar; 33 }
原文:https://www.cnblogs.com/jeffid/p/8904786.html