🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
### 不同时间范围选择时间插件封装 **位置:** Application\Common\View\cycle_date.html **效果:** ![](https://img.kancloud.cn/16/1f/161ff05d22935b497afca57eae92172e_822x67.png) **View Form 搜索条件中调用:** ### View : `<include file="./Application/Common/View/cycle_date.html" /> ` ### Controller: ```` //传参数到时间控件控制显示 //1、今天,2、昨天,3、本周,4、上周,5、本月,6、上月,7、本季、8、上季,9、本年 $this->assign('date',array(1,2,3,4,5,6,7,8,9)); <!--周期时间--> <input class="form-control calendar-icon" placeholder="开始日期" readonly name="start_date" id="date_start" value="{$params.start_date}"> <input class="form-control calendar-icon" placeholder="结束日期" readonly name="end_date" id="date_end" value="{$params.end_date}"> <volist name="date" id="vo"> <if condition="$vo eq 1"> <input class="btn btn-default" type="button" onclick="get_date(1)" value="今天"> </if> <if condition="$vo eq 2"> <input class="btn btn-default" type="button" onclick="get_date(2)" value="昨天"> </if> <if condition="$vo eq 3"> <input class="btn btn-default" type="button" onclick="get_date(3)" value="本周"> </if> <if condition="$vo eq 4"> <input class="btn btn-default" type="button" onclick="get_date(4)" value="上周"> </if> <if condition="$vo eq 5"> <input class="btn btn-default" type="button" onclick="get_date(5)" value="本月"> </if> <if condition="$vo eq 6"> <input class="btn btn-default" type="button" onclick="get_date(6)" value="上月"> </if> <if condition="$vo eq 7"> <input class="btn btn-default" type="button" onclick="get_date(7)" value="本季"> </if> <if condition="$vo eq 8"> <input class="btn btn-default" type="button" onclick="get_date(8)" value="上季"> </if> <if condition="$vo eq 9"> <input class="btn btn-default" type="button" onclick="get_date(9)" value="本年"> </if> </volist> <script> function get_date(type) { switch(type) { // 今日 case 1: var start = dateFtt("yyyy-MM-dd",dateRangeUtil.getCurrentDate()); var end = dateFtt("yyyy-MM-dd",dateRangeUtil.getCurrentDate()); break; // 昨天 case 2: var start = dateFtt("yyyy-MM-dd",dateRangeUtil.getYesterday()); var end = dateFtt("yyyy-MM-dd",dateRangeUtil.getYesterday()); break; // 本周 case 3: var start = dateFtt("yyyy-MM-dd",dateRangeUtil.getCurrentWeek()[0]); var end = dateFtt("yyyy-MM-dd",dateRangeUtil.getCurrentWeek()[1]); break; // 上周 case 4: var start = dateFtt("yyyy-MM-dd",dateRangeUtil.getPreviousWeek()[0]); var end = dateFtt("yyyy-MM-dd",dateRangeUtil.getPreviousWeek()[1]); break; // 本月 case 5: var start = dateFtt("yyyy-MM-dd",dateRangeUtil.getCurrentMonth()[0]); var end = dateFtt("yyyy-MM-dd",dateRangeUtil.getCurrentMonth()[1]); break; // 上月 case 6: var start = dateFtt("yyyy-MM-dd",dateRangeUtil.getPreviousMonth()[0]); var end = dateFtt("yyyy-MM-dd",dateRangeUtil.getPreviousMonth()[1]); break; // 本季 case 7: var start = dateFtt("yyyy-MM-dd",dateRangeUtil.getCurrentSeason()[0]); var end = dateFtt("yyyy-MM-dd",dateRangeUtil.getCurrentSeason()[1]); break; // 上季 case 8: var start = dateFtt("yyyy-MM-dd",dateRangeUtil.getLastSeason()[0]); var end = dateFtt("yyyy-MM-dd",dateRangeUtil.getLastSeason()[1]); break; // 本年 case 9: var start = dateFtt("yyyy-MM-dd",dateRangeUtil.getCurrentYear()[0]); var end = dateFtt("yyyy-MM-dd",dateRangeUtil.getCurrentYear()[1]); break; default: } $('#date_start').val(start); $('#date_end').val(end); } //js时间戳转换日期格式 function dateFtt(fmt,date) { //author: meizz var o = { "M+" : date.getMonth()+1, //月份 "d+" : date.getDate(), //日 "h+" : date.getHours(), //小时 "m+" : date.getMinutes(), //分 "s+" : date.getSeconds(), //秒 "q+" : Math.floor((date.getMonth()+3)/3), //季度 "S" : date.getMilliseconds() //毫秒 }; if(/(y+)/.test(fmt)) fmt=fmt.replace(RegExp.$1, (date.getFullYear()+"").substr(4 - RegExp.$1.length)); for(var k in o) if(new RegExp("("+ k +")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length))); return fmt; } //修改月、天的格式,保持两位数显示 function two(m){ return m<10?'0'+m:m } /** * 日期范围工具类 */ var dateRangeUtil = (function () { /*** * 获得当前时间 */ this.getCurrentDate = function () { return new Date(); }; this.getYesterday = function () { var today = new Date(); var oneday = 1000 * 60 * 60 * 24; return new Date(today - oneday); } /*** * 获得本周起止时间 */ this.getCurrentWeek = function () { //起止日期数组 var startStop = new Array(); //获取当前时间 var currentDate = this.getCurrentDate(); //返回date是一周中的某一天 var week = currentDate.getDay(); //返回date是一个月中的某一天 var month = currentDate.getDate(); //一天的毫秒数 var millisecond = 1000 * 60 * 60 * 24; //减去的天数 var minusDay = week != 0 ? week - 1 : 6; //alert(minusDay); //本周 周一 var monday = new Date(currentDate.getTime() - (minusDay * millisecond)); //本周 周日 if (currentDate < (monday.getTime() + (6 * millisecond))){ var sunday = new Date(currentDate); }else {var sunday = new Date(monday.getTime() + (6 * millisecond)); var sunday = new Date(monday.getTime() + (6 * millisecond)); } //添加本周时间 startStop.push(monday); //本周起始时间 //添加本周最后一天时间 startStop.push(sunday); //本周终止时间 //返回 return startStop; }; /*** * 获得本月的起止时间 */ this.getCurrentMonth = function () { //起止日期数组 var startStop = new Array(); //获取当前时间 var currentDate = this.getCurrentDate(); //获得当前月份0-11 var currentMonth = currentDate.getMonth(); //获得当前年份4位年 var currentYear = currentDate.getFullYear(); //求出本月第一天 var firstDay = new Date(currentYear, currentMonth, 1); //当为12月的时候年份需要加1 //月份需要更新为0 也就是下一年的第一个月 if (currentMonth == 11) { currentYear++; currentMonth = 0; //就为 } else { //否则只是月份增加,以便求的下一月的第一天 currentMonth++; } //一天的毫秒数 var millisecond = 1000 * 60 * 60 * 24; //下月的第一天 var nextMonthDayOne = new Date(currentYear, currentMonth, 1); //求出上月的最后一天 if (currentDate > lastDay) { var lastDay = new Date(nextMonthDayOne.getTime() - millisecond); }else { var lastDay = currentDate; } //添加至数组中返回 startStop.push(firstDay); startStop.push(lastDay); //返回 return startStop; }; /** * 得到本季度开始的月份 * @param month 需要计算的月份 ***/ this.getQuarterSeasonStartMonth = function (month) { var quarterMonthStart = 0; var spring = 0; //春 var summer = 3; //夏 var fall = 6; //秋 var winter = 9; //冬 //月份从0-11 if (month < 3) { return spring; } if (month < 6) { return summer; } if (month < 9) { return fall; } return winter; }; /** * 获得本季度的起止日期 */ this.getCurrentSeason=function(){ //起止日期数组 var startStop=new Array(); //获取当前时间 var currentDate=this.getCurrentDate(); //获得当前月份0-11 var currentMonth=currentDate.getMonth(); //获得当前年份4位年 var currentYear=currentDate.getFullYear(); //获得本季度开始月份 var quarterSeasonStartMonth=this.getQuarterSeasonStartMonth(currentMonth); //获得本季度结束月份 var quarterSeasonEndMonth=quarterSeasonStartMonth+2; //获得本季度开始的日期 var quarterSeasonStartDate=new Date(currentYear,quarterSeasonStartMonth,1); //获得本季度结束的日期 var quarterSeasonEndDate=new Date(currentYear,quarterSeasonEndMonth,this.getMonthDays(currentYear, quarterSeasonEndMonth)); if (currentDate < quarterSeasonEndDate) { quarterSeasonEndDate = currentDate; } //加入数组返回 startStop.push(quarterSeasonStartDate); startStop.push(quarterSeasonEndDate); //返回 return startStop; }; /** * 获得上季度的起止日期 */ this.getLastSeason=function(){ //起止日期数组 var startStop=new Array(); //获取当前时间 var currentDate=this.getCurrentDate(); //获得当前月份0-11 var currentMonth=currentDate.getMonth(); //获得当前年份4位年 var currentYear=currentDate.getFullYear(); //获得本季度开始月份 var quarterSeasonStartMonth=this.getQuarterSeasonStartMonth(currentMonth)-3; //获得本季度结束月份 var quarterSeasonEndMonth=quarterSeasonStartMonth+2; //获得本季度开始的日期 var quarterSeasonStartDate=new Date(currentYear,quarterSeasonStartMonth,1); //获得本季度结束的日期 var quarterSeasonEndDate=new Date(currentYear,quarterSeasonEndMonth,this.getMonthDays(currentYear, quarterSeasonEndMonth)); //加入数组返回 startStop.push(quarterSeasonStartDate); startStop.push(quarterSeasonEndDate); //返回 return startStop; }; /** * 获得该月的天数 * @param year年份 * @param month月份 * */ this.getMonthDays = function (year, month) { //本月第一天 1-31 var relativeDate = new Date(year, month, 1); //获得当前月份0-11 var relativeMonth = relativeDate.getMonth(); //获得当前年份4位年 var relativeYear = relativeDate.getFullYear(); //当为12月的时候年份需要加1 //月份需要更新为0 也就是下一年的第一个月 if (relativeMonth == 11) { relativeYear++; relativeMonth = 0; } else { //否则只是月份增加,以便求的下一月的第一天 relativeMonth++; } //一天的毫秒数 var millisecond = 1000 * 60 * 60 * 24; //下月的第一天 var nextMonthDayOne = new Date(relativeYear, relativeMonth, 1); //返回得到上月的最后一天,也就是本月总天数 return new Date(nextMonthDayOne.getTime() - millisecond).getDate(); }; /*** * 得到本年的起止日期 * */ this.getCurrentYear = function () { //起止日期数组 var startStop = new Array(); //获取当前时间 var currentDate = this.getCurrentDate(); //获得当前年份4位年 var currentYear = currentDate.getFullYear(); //本年第一天 var currentYearFirstDate = new Date(currentYear, 0, 1); if (currentDate > currentYearLastDate) { var currentYearLastDate = new Date(currentYear, 11, 31); }else { var currentYearLastDate = currentDate; } //添加至数组 startStop.push(currentYearFirstDate); startStop.push(currentYearLastDate); //返回 return startStop; }; /** * 返回上一个月的第一天Date类型 * @param year 年 * @param month 月 **/ this.getPriorMonthFirstDay = function (year, month) { //年份为0代表,是本年的第一月,所以不能减 if (month == 0) { month = 11; //月份为上年的最后月份 year--; //年份减1 return new Date(year, month, 1); } //否则,只减去月份 month--; return new Date(year, month, 1); ; }; /** * 获得上一月的起止日期 * ***/ this.getPreviousMonth = function () { //起止日期数组 var startStop = new Array(); //获取当前时间 var currentDate = this.getCurrentDate(); //获得当前月份0-11 var currentMonth = currentDate.getMonth(); //获得当前年份4位年 var currentYear = currentDate.getFullYear(); //获得上一个月的第一天 var priorMonthFirstDay = this.getPriorMonthFirstDay(currentYear, currentMonth); //获得上一月的最后一天 var priorMonthLastDay = new Date(priorMonthFirstDay.getFullYear(), priorMonthFirstDay.getMonth(), this.getMonthDays(priorMonthFirstDay.getFullYear(), priorMonthFirstDay.getMonth())); //添加至数组 startStop.push(priorMonthFirstDay); startStop.push(priorMonthLastDay); //返回 return startStop; }; /** * 获得上一周的起止日期 * **/ this.getPreviousWeek = function () { //起止日期数组 var startStop = new Array(); //获取当前时间 var currentDate = this.getCurrentDate(); //返回date是一周中的某一天 var week = currentDate.getDay(); //返回date是一个月中的某一天 var month = currentDate.getDate(); //一天的毫秒数 var millisecond = 1000 * 60 * 60 * 24; //减去的天数 var minusDay = week != 0 ? week - 1 : 6; //获得当前周的第一天 var currentWeekDayOne = new Date(currentDate.getTime() - (millisecond * minusDay)); //上周最后一天即本周开始的前一天 var priorWeekLastDay = new Date(currentWeekDayOne.getTime() - millisecond); //上周的第一天 var priorWeekFirstDay = new Date(priorWeekLastDay.getTime() - (millisecond * 6)); //添加至数组 startStop.push(priorWeekFirstDay); startStop.push(priorWeekLastDay); return startStop; }; return this; })(); </script> ~~~