获取季度、月、周时间开始及结束时间,结束时间不超过当前时间

/*本季度、本月、上季度、上月点击事件*/
    timeRange:function(that,judge){
        var ds = new Date();
        var year = ds.getFullYear();
        var month = ds.getMonth();
        var date = ds.getDate();
        $('.curr-season').click(function () {  //本季度
            startTime = that.getQuarterMonth(0,"s");
            var _endTime = that.getQuarterMonth(0,"e");
            //获取当前日期
            var month = ds.getMonth() + 1,
            month = month >= 10 ? month : '0' + month;
            date1 = date >= 10 ? date : '0' + date;
            var currTime = new Date();
            var endDate = new Date(_endTime);
            //比较当前日期跟周结束日期大小
            if(currTime.getTime() > endDate.getTime()){
                endTime = _endTime;
            }else{
                endTime = year + '-' + month + '-' + date1;
            }
            if(judge){
                $('#test16').val(startTime+' 到 '+endTime);
            }
        });

        $('.last-season').click(function () {  //上季度
            startTime = that.getQuarterMonth(1,"s");
            endTime = that.getQuarterMonth(1,"e");
            if(judge){
                $('#test16').val(startTime+' 到 '+endTime);
            }
        });
        $('.curr-month').click(function () {  //本月
            var month = ds.getMonth() + 1;
            month = month >= 10 ? month : '0' + month;
            date1 = date >= 10 ? date : '0' + date;
            var str = year + '-' + month + '-01' + ' 到 ' + year + '-' + month + '-' + date1;
            $("#test16").val(str);
            $('.layui-laydate-range').remove();
            startTime = str.substr(0,10);
            endTime = str.substr(13);
            if(judge){
                $('#test16').val(startTime+' 到 '+endTime);
            }
        });
        $('.last-month').click(function () {  //上月
            if (month == 0) {
                month = 11;
                year = year - 1;
            }
            month = month >= 10 ? month : '0' + month;
            var curMonthDays = new Date(ds.getFullYear(), (ds.getMonth()), 0).getDate();
            var str = year + '-' + month + '-01' + ' 到 ' + year + '-' + month + '-' + curMonthDays;
            $("#test16").val(str);
            $('.layui-laydate-range').remove();
            startTime = str.substr(0,10);
            endTime = str.substr(13);
            if(judge){
                $('#test16').val(startTime+' 到 '+endTime);
            }
        }); 
       
        $('.curr-week').click(function () {  //本周
            startTime = that._getTime(0);//周开始时间
            var _endTime=that._getTime(-6);//周结束时间
            //获取当前日期
            var month = ds.getMonth() + 1,
            month = month >= 10 ? month : '0' + month;
            date1 = date >= 10 ? date : '0' + date;
            var currTime = new Date();
            var endDate = new Date(_endTime);
            //比较当前日期跟周结束日期大小
            if(currTime.getTime() > endDate.getTime()){
                endTime = _endTime;
            }else{
                endTime = year + '-' + month + '-' + date1;
            }
            if(judge){
                $('#test16').val(startTime+' 到 '+endTime);
            }
            $('#test16').val(startTime+' 到 '+endTime);
        });
    },
    /*获取季度区间*/
    getQuarterMonth:function(btn,type){  
        var now = new Date(); 
        var year = now.getFullYear();
        if(btn == "0"){
            var nowMonth = now.getMonth();
        }else{
            var nowMonth = now.getMonth()-3;
            if (nowMonth <= 0) {
                nowMonth = nowMonth + 12;
                year = year - 1;
            }
        }
        var quarterStartMonth = 1;     
        if(nowMonth<3){     
           quarterStartMonth = 1;     
        }     
        if(2<nowMonth && nowMonth<6){     
           quarterStartMonth = 4;     
        }     
        if(5<nowMonth && nowMonth<9){     
           quarterStartMonth = 7;     
        }     
        if(nowMonth>8){     
           quarterStartMonth = 10;     
        } 
        if (type == "s") {
            var day = year+"-"+quarterStartMonth+"-01"; 
        };//1-3 4-6 7-9 10-12
        if (type == "e") {
            if(quarterStartMonth==1){
                var day = year+"-"+(quarterStartMonth+2)+"-31";
            }
            if(quarterStartMonth==10){
                var day = year+"-"+(quarterStartMonth+2)+"-31";
            }
            if(quarterStartMonth==4){
                var day = year+"-"+(quarterStartMonth+2)+"-30";
            }
            if(quarterStartMonth==7){
                var day = year+"-"+(quarterStartMonth+2)+"-30";
            }
        };
        return day;     
    },
    /*获取周区间*/
    _getTime:function(n){
        var now=new Date();
        var year=now.getFullYear();
        //因为月份是从0开始的,所以获取这个月的月份数要加1才行
        var month=now.getMonth()+1;	
        var date=now.getDate();
        var day=now.getDay();
        //判断是否为周日,如果不是的话,就让今天的day-1(例如星期二就是2-1)
        if(day!= 0){
            n=n+(day-1);
        }else{
            n=n+day;
        }
        if(day){//这个判断是为了解决跨年的问题
            if(month>1){
                month=month;
            }else{//这个判断是为了解决跨年的问题,月份是从0开始的
                year=year-1;
                month=12;
            }
        }
        now.setDate(now.getDate()-n);	
        year=now.getFullYear();
        month=now.getMonth()+1;
        date=now.getDate();

        s=year+"-"+(month<10?('0'+month):month)+"-"+(date<10?('0'+date):date);
        
        return s;
    },

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值