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="cw">本周</option>
        <option value="pw">上周</option>
        <option value="cm">本月</option>
        <option value="pm">上月</option>
        <option value="cq">本季</option>
        <option value="pq">上季</option>
        <option value="cy">本年</option>
        <option value="py">上年</option>
    </select><br /><br/>
    <input type="text" name="name" value=" " id="sDate" />至
    <input type="text" name="name" value=" " id="eDate" />
时间: 2024-10-12 12:29:52

JavaScript实现Select选择本周、上周、本季、上季、本年的时间选取的相关文章

javascript的时间段选择

最近要在页面上加一个时间段的选择控件,便上网搜寻一番,但找到的答案大都没有考虑时间段跨年的情况,所以做了些修改 本周的开始和结束日期 ? 1 2 3 4 5 6 7 8 9 //当前日期 var now = new Date(); //今天是一周的第几天 var nowDayForWeek = now.getDay() === 0 ? 7 : now.getDay(); var oneDay = 1000 * 60 * 60 * 24; //本周开始日期 var weekStartDay = n

jQuery获取Select选择的Text(非表单元素)和 Value(表单元素)(转)

jQuery获取Select选择的Text和Value: 语法解释: 1. $("#select_id").change(function(){//code...}); //为Select添加事件,当选择其中一项时触发 2. var checkText=$("#select_id").find("option:selected").text(); //获取Select选择的Text 3. var checkValue=$("#selec

jQuery获取Select选择的Text和 Value

jQuery获取Select选择的Text和Value: 语法解释: 1. $("#select_id").change(function(){//code...});   //为Select添加事件,当选择其中一项时触发 2. var checkText=$("#select_id").find("option:selected").text();  //获取Select选择的Text 3. var checkValue=$("#se

jQuery设置Select选择的 Text和Value

语法解释: 复制代码代码如下: 1. $("#select_id").change(function(){//code...}); //为Select添加事件,当选择其中一项时触发 2. var checkText=$("#select_id").find("option:selected").text(); //获取Select选择的Text 3. var checkValue=$("#select_id").val();

jQuery获取Select选择的Text和 Value(转)

jQuery获取Select选择的Text和Value:语法解释:1. $("#select_id").change(function(){//code...});   //为Select添加事件,当选择其中一项时触发2. var checkText=$("#select_id").find("option:selected").text();  //获取Select选择的Text3. var checkValue=$("#select

jQuery获取Select选择的Text和Value(详细汇总)

语法解释: 1. $("#select_id").change(function(){//code...}); //为Select添加事件,当选择其中一项时触发 2. var checkText=$("#select_id").find("option:selected").text(); //获取Select选择的Text 3. var checkValue=$("#select_id").val(); //获取Select

Oracle 获取本周、本月、本季、本年的第一天和最后一天

Oracle 获取本周.本月.本季.本年的第一天和最后一天 --本周 select trunc(sysdate, 'd') + 1 from dual; select trunc(sysdate, 'd') + 7 from dual; --本月 select trunc(sysdate, 'mm') from dual; select last_day(trunc(sysdate)) from dual; --本季 select trunc(sysdate, 'Q') from dual; s

Android较低版本(&lt;5.2) 页面默认Select选择框效果的BUG解决

Bug描述: 使用低版本安卓(<5.2),在微信上打开网页,点击下拉框,会出现如下图所示的用来展示select选项的弹出框: 在选项较少的时候,可以向下滑动,将选项滑到底部 滑动前: 滑动后: 期望达到的效果: 解决方案: 判断是否是微信环境: function isWeixinBrowser(){ return /micromessenger/.test(navigator.userAgent.toLowerCase()); } 判断安卓版号: var userAgent = navigato

jQuery获取Select选择的Text和Value[转载]

语法解释:1. $("#select_id").change(function(){//code...});   //为Select添加事件,当选择其中一项时触发2. var checkText=$("#select_id").find("option:selected").text();  //获取Select选择的Text3. var checkValue=$("#select_id").val();  //获取Selec