首页 > 代码库 > JavaScript实现Select选择本周、上周、本季、上季、本年的时间选取
JavaScript实现Select选择本周、上周、本季、上季、本年的时间选取
//转自http://blog.csdn.net/xc_young/article/details/16878849,其上季度有错误,已修改 var MrYangUtil = function () { this.getCurrentDate = function () { return new Date(); }; 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)); //本周 周日 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); //求出上月的最后一天 var lastDay = new Date(nextMonthDayOne.getTime() - millisecond); //添加至数组中返回 startStop.push(firstDay); startStop.push(lastDay); //返回 return startStop; }; 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.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.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)); //加入数组返回 startStop.push(quarterSeasonStartDate); startStop.push(quarterSeasonEndDate); //返回 return startStop; }; this.getCurrentYear = function () { //起止日期数组 var startStop = new Array(); //获取当前时间 var currentDate = this.getCurrentDate(); //获得当前年份4位年 var currentYear = currentDate.getFullYear(); //本年第一天 var currentYearFirstDate = new Date(currentYear, 0, 1); //本年最后一天 var currentYearLastDate = new Date(currentYear, 11, 31); //添加至数组 startStop.push(currentYearFirstDate); startStop.push(currentYearLastDate); //返回 return startStop; }; 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; }; this.getPriorSeasonFirstDay = function (year, month) { var quarterMonthStart = 0; var spring = 0; //春 var summer = 3; //夏 var fall = 6; //秋 var winter = 9;//冬 //月份从0-11 switch (Math.floor(month/3)) {//季度的其实月份 case 0: //如果是第一季度则应该到去年的冬季 year--; month = winter; break; case 1: month = spring; break; case 2: month = summer; break; case 3: month = fall; break; }; return new Date(year, month, 1); }; this.getPreviousSeason = function () { //起止日期数组 var startStop = new Array(); //获取当前时间 var currentDate = this.getCurrentDate(); //获得当前月份0-11 var currentMonth = currentDate.getMonth(); //获得当前年份4位年 var currentYear = currentDate.getFullYear(); //上季度的第一天 var priorSeasonFirstDay = this.getPriorSeasonFirstDay(currentYear, currentMonth); //上季度的最后一天 var priorSeasonLastDay = new Date(priorSeasonFirstDay.getFullYear(), priorSeasonFirstDay.getMonth() + 2, this.getMonthDays(priorSeasonFirstDay.getFullYear(), priorSeasonFirstDay.getMonth() + 2)); //添加至数组 startStop.push(priorSeasonFirstDay); startStop.push(priorSeasonLastDay); return startStop; }; this.getPreviousYear = function () { //起止日期数组 var startStop = new Array(); //获取当前时间 var currentDate = this.getCurrentDate(); //获得当前年份4位年 var currentYear = currentDate.getFullYear(); currentYear--; var priorYearFirstDay = new Date(currentYear, 0, 1); var priorYearLastDay = new Date(currentYear, 11, 31); //添加至数组 startStop.push(priorYearFirstDay); startStop.push(priorYearLastDay); return startStop; }; };
//csdn 的 Meizz /** * 对Date的扩展,将 Date 转化为指定格式的String * 月(M)、日(d)、12小时(h)、24小时(H)、分(m)、秒(s)、周(E)、季度(q) 可以用 1-2 个占位符 * 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字) * eg: * (new Date()).pattern("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423 * (new Date()).pattern("yyyy-MM-dd E HH:mm:ss") ==> 2009-03-10 二 20:09:04 * (new Date()).pattern("yyyy-MM-dd EE hh:mm:ss") ==> 2009-03-10 周二 08:09:04 * (new Date()).pattern("yyyy-MM-dd EEE hh:mm:ss") ==> 2009-03-10 星期二 08:09:04 * (new Date()).pattern("yyyy-M-d h:m:s.S") ==> 2006-7-2 8:9:4.18 */ Date.prototype.pattern = function (fmt) { var o = { "M+": this.getMonth() + 1, //月份 "d+": this.getDate(), //日 "h+": this.getHours() % 12 == 0 ? 12 : this.getHours() % 12, //小时 "H+": this.getHours(), //小时 "m+": this.getMinutes(), //分 "s+": this.getSeconds(), //秒 "q+": Math.floor((this.getMonth() + 3) / 3), //季度 "S": this.getMilliseconds() //毫秒 }; var week = { "0": "/u65e5", "1": "/u4e00", "2": "/u4e8c", "3": "/u4e09", "4": "/u56db", "5": "/u4e94", "6": "/u516d" }; if (/(y+)/.test(fmt)) { fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length)); } if (/(E+)/.test(fmt)) { fmt = fmt.replace(RegExp.$1, ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? "/u661f/u671f" : "/u5468") : "") + week[this.getDay() + ""]); } 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; }
var my = new MrYangUtil(); window.onload = function () { var sIpt = document.getElementById(‘sDate‘); var eIpt = document.getElementById(‘eDate‘); document.getElementById(‘sel‘).onchange = function () { switch (this.value) { case "cw": sIpt.value = my.getCurrentWeek()[0].pattern("yyyy-MM-dd"); eIpt.value = my.getCurrentWeek()[1].pattern("yyyy-MM-dd"); break; case "pw": sIpt.value = my.getPreviousWeek()[0].pattern("yyyy-MM-dd"); eIpt.value = my.getPreviousWeek()[1].pattern("yyyy-MM-dd"); break; case "cm": sIpt.value = my.getCurrentMonth()[0].pattern("yyyy-MM-dd"); eIpt.value = my.getCurrentMonth()[1].pattern("yyyy-MM-dd"); break; case "pm": sIpt.value = my.getPreviousMonth()[0].pattern("yyyy-MM-dd"); eIpt.value = my.getPreviousMonth()[1].pattern("yyyy-MM-dd"); break; case "cq": sIpt.value = my.getCurrentSeason()[0].pattern("yyyy-MM-dd"); eIpt.value = my.getCurrentSeason()[1].pattern("yyyy-MM-dd"); break; case "pq": sIpt.value = my.getPreviousSeason()[0].pattern("yyyy-MM-dd"); eIpt.value = my.getPreviousSeason()[1].pattern("yyyy-MM-dd"); break; case "cy": sIpt.value = my.getCurrentYear()[0].pattern("yyyy-MM-dd"); eIpt.value = my.getCurrentYear()[1].pattern("yyyy-MM-dd"); break; case "py": sIpt.value = my.getPreviousYear()[0].pattern("yyyy-MM-dd"); eIpt.value = my.getPreviousYear()[1].pattern("yyyy-MM-dd"); break; default: break; } }; };
<select id="sel"> <option value="http://www.mamicode.com/cw">本周</option> <option value="http://www.mamicode.com/pw">上周</option> <option value="http://www.mamicode.com/cm">本月</option> <option value="http://www.mamicode.com/pm">上月</option> <option value="http://www.mamicode.com/cq">本季</option> <option value="http://www.mamicode.com/pq">上季</option> <option value="http://www.mamicode.com/cy">本年</option> <option value="http://www.mamicode.com/py">上年</option> </select><br /><br/> <input type="text" name="name" value="http://www.mamicode.com/" id="sDate" />至 <input type="text" name="name" value="http://www.mamicode.com/" id="eDate" />
JavaScript实现Select选择本周、上周、本季、上季、本年的时间选取
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。