1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
| /** * [getCalendarMonth 获得指定日期所在月的第一周到第四/五周的数据组合,形如: * [{ "date": "2016/10/30", //日期字符串 "dateNum": 30, //日 "isCurMonth": false, //是否当前月 "weekIndex": 0 //是本月的第几周,下标从0开始 },{ "date": "2016/10/31", "dateNum": 31, "isCurMonth": false, "weekIndex": 0 },{ "date": "2016/11/1", "dateNum": 1, "day": 2, "isCurMonth": true, "isToday": false, "weekIndex": 0 }] ] * @param {[Date]} param [指定的日期,非必填,默认为当天] * @return {[Array]} [获得指定日期所在月的第一周到第四/五周的数据组合] */ export function getCalendarMonth(date) { date = date ? date : new Date(); let y = date.getFullYear(); let m = date.getMonth(); let _m; let firstDay = new Date(y, m, 1).getDay(); //当月第一天 周期 let days = monthSize(date);//当月天数 let prevMonthDays = monthSize(new Date(y, m - 1));//上月天数 let initPrevDay = prevMonthDays - firstDay; let lines = Math.ceil((firstDay + days) / 7); _m = new Array(lines * 7); let nextMonthDay = 0;
for (let i = 0; i < _m.length; i++) { let weekIndex = parseInt(i / 7); if (i < firstDay) { let date = ++initPrevDay; if (m === 0 && date > 7) { _m[i] = { isCurMonth: false, dateNum: date, weekIndex, date: `${y - 1}/${12}/${date}` }; } else { _m[i] = { isCurMonth: false, dateNum: date, weekIndex, date: `${y}/${m}/${date}` }; } } else if (i >= (firstDay + days)) { let date = ++nextMonthDay;
if (m === 11 && date <= 7) { _m[i] = { isCurMonth: false, dateNum: date, weekIndex, date: `${y + 1}/${1}/${date}` }; } else { _m[i] = { isCurMonth: false, dateNum: date, weekIndex, date: `${y}/${m + 2}/${date}` }; } } else { let _date = i - firstDay + 1; let today = new Date(); let today_y = today.getFullYear(); let today_m = today.getMonth(); let today_d = today.getDate(); let isToday = today_y === y && today_m === m && today_d === _date ? true : false; _m[i] = { dateNum: _date, //日期 day: i % 7, //周期 weekIndex, isCurMonth: true, isToday, date: `${y}/${m + 1}/${_date}` }; } } return _m; }
|