首页 > 代码库 > 生成按月递增循环日期
生成按月递增循环日期
工具方法: // 日期加法 DateAdd(strInterval, Number) { let dtTmp = this; switch (strInterval) { case ‘s‘ :return new Date(Date.parse(dtTmp) + (1000 * Number)); break; case ‘n‘ :return new Date(Date.parse(dtTmp) + (60000 * Number)); break; case ‘h‘ :return new Date(Date.parse(dtTmp) + (3600000 * Number)); break; case ‘d‘ :return new Date(Date.parse(dtTmp) + (86400000 * Number)); break; case ‘w‘ :return new Date(Date.parse(dtTmp) + ((86400000 * 7) * Number)); break; case ‘q‘ :return new Date(dtTmp.getFullYear(), (dtTmp.getMonth()) + Number*3, dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds()); break; case ‘m‘ :return new Date(dtTmp.getFullYear(), (dtTmp.getMonth()) + Number, dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds()); break; case ‘y‘ :return new Date((dtTmp.getFullYear() + Number), dtTmp.getMonth(), dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds()); break; default: return dtTmp; } }; // 判断某一天是否为该月最后一天 isMaxDay(){ let isMaxDay = false; let time = new Date(this); let month = time.getMonth(); let nextDay = time.DateAdd(‘d‘,1); let nextDayMonth = nextDay.getMonth(); if(nextDayMonth - month!=0) { isMaxDay = true; } return isMaxDay; }; // 日期减法 DateSubtract(strInterval, Number) { let dtTmp = this; switch (strInterval) { case ‘s‘ :return new Date(Date.parse(dtTmp) - (1000 * Number)); break; case ‘n‘ :return new Date(Date.parse(dtTmp) - (60000 * Number)); break; case ‘h‘ :return new Date(Date.parse(dtTmp) - (3600000 * Number)); break; case ‘d‘ :return new Date(Date.parse(dtTmp) - (86400000 * Number)); break; case ‘w‘ :return new Date(Date.parse(dtTmp) - ((86400000 * 7) * Number)); break; default: return dtTmp; } }; 逻辑方法: pusharr(times,date){ this.state.arr.push(date); // 判断该天是否为改月最后一天,如果是走第一种算法 if(date.isMaxDay()){ let nextTime = ‘‘; for (let i = 1; i < times; i++) { nextTime = date.DateAdd(‘d‘,31); let basicM = date.getMonth(); let nextM = nextTime.getMonth(); if(nextM - basicM ===2) { let nextD = nextTime.getDate(); nextTime = nextTime.DateSubtract(‘d‘,nextD); } date = nextTime; this.state.arr.push(nextTime); console.log(this.state.arr); } } // 该天不是该月最后一天,走第二种算法 else { let nextTime = ‘‘; for (let i = 1; i<times;i++) { nextTime = date.DateAdd(‘m‘,i); let basicM = date.getMonth(); let nextM = nextTime.getMonth(); if (nextM - basicM ===i+1) { let nextD = nextTime.getDate(); nextTime = nextTime.DateSubtract(‘d‘,nextD); } else if (nextM - basicM <=0) { times = times -i; this.pusharr(times,nextTime); break; } this.state.arr.push(nextTime); console.log(this.state.arr); } } },
生成按月递增循环日期
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。