1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- /* 类型:week(按周),month(按月);
- num: 一次几周,一次几个月
- day指定时间:1~7(按周) 或 1~12(按月)
- */
- function getStartDate(type, day) {
- if(type === "week") {
- // 当前周
- var curDay = moment().isoWeekday();
- if(curDay >= day) {
- return moment().isoWeekday(day+7);
- } else {
- return moment().isoWeekday(day);
- }
- } else if(type === "month"){
- // 当前年份
- var curYear = moment().year();
- // 当前月份
- var curMonth = moment().month() + 1;
- // 当前日期
- var curDate = moment().date();
- if(curDate >= day) {
- return moment({year: curYear, month: curMonth, date : day}); // 注:month是从0开始算,范围:0~11
- } else {
- return moment({year: curYear, month: curMonth - 1, date : day});
- }
- }
- }
-
- /* 按周获取日期(最近3个月内)
- num: 一次几周
- day指定时间:1~7
- */
- function getFreDateArrByWeek(num, day) {
- // 开始时间
- var startDate = getStartDate("week",day);
- // 3个月后的截止时间
- var maxDate = startDate.clone().month(startDate.month() + 3);
- // 符合条件的3个月内的日期数组
- var result = [];
- var buff = startDate.clone();
- while(buff.isSameOrBefore(maxDate)) {
- result.push(buff.clone());
- buff.isoWeekday(buff.isoWeekday()+7*num);
- }
-
- return result;
- }
-
- /* 按周获取日期(最近12个月内)
- num: 一次几个月
- day指定时间:1~28
- */
- function getFreDateArrByMonth(num, day) {
- // 开始时间
- var startDate = getStartDate("month",day);
- // 12个月后的截止时间
- var maxDate = startDate.clone().month(startDate.month() + 12);
- // 符合条件的12个月内的日期数组
- var result = [];
- var buff = startDate.clone();
- while(buff.isSameOrBefore(maxDate)) {
- result.push(buff.clone());
- buff.month(buff.month()+num);
- }
-
- return result;
- }
-
- /* fre频率:weekN(N周/次),monthN(N月/次);
- day指定时间:1~7(按周) 或 1~12(按月)
- */
- function getFreDateArr(fre, day) {
- var reg = /^(week|month)(\d+)$/,
- types = reg.exec(fre),
- type = types[1],
- num = +types[2];
-
- if(type === "week") {
- return getFreDateArrByWeek(num, day);
- } else if(type === "month" && day <= 28) {
- return getFreDateArrByMonth(num, day);
- }
-
- throw new Error("参数有误!")
- }
|