zoukankan      html  css  js  c++  java
  • js 获取近一个月 近三个月近半年的时间范围

    // 获取上个月的时间
        getLastMonth() {
          var nowdays = new Date();
          var year = nowdays.getFullYear();
          var month = nowdays.getMonth();
          if (month == 0) {
            month = 12;
            year = year - 1;
          }
          if (month < 10) {
            month = '0' + month;
          }
          var myDate = new Date(year, month, 0);
          var startDate = year + '-' + month + '-01'; //上个月第一天
          var endDate = year + '-' + month + '-' + myDate.getDate() ; //上个月最后一天
          this.setData({
            'options[0].value':startDate+'-'+endDate
          })
        },
        // 获取近三个月 往前推90天
        getLastthreeMonth(){
          var now = new Date();
          var year = now.getFullYear();
          //0-11表示1-12月
          var month = now.getMonth() + 1;
          var day = now.getDate();
          var dateObj = {};
          dateObj.now = year + '-' + month + '-' + day;
          //当前月的总天数
          var nowMonthDay = new Date(year, month, 0).getDate();
          //如果是1、2、3月,年数往前推一年    
          if(month - 3 <= 0){ 
              //3个月前所在月的总天数
              var last3MonthDay = new Date((year - 1), (12 - (3 - parseInt(month))), 0).getDate();
              //3个月前所在月的总天数小于现在的天日期    
              if(last3MonthDay < day){    
                  dateObj.last = (year - 1) + '-' + (12 - (3 - month)) + '-' + last3MonthDay;
              }else{
                  dateObj.last = (year - 1) + '-' + (12 - (3 - month)) + '-' + day;
              }
          }else{
            //3个月前所在月的总天数
              var last3MonthDay = new Date(year, (parseInt(month) - 3), 0).getDate(); 
              //3个月前所在月的总天数小于现在的天日期   
              if(last3MonthDay < day){    
                 //当前天日期小于当前月总天数,2月份比较特殊的月份
                  if(day < nowMonthDay){       
                      dateObj.last = year + '-' + (month - 3) + '-' + (last3MonthDay - (nowMonthDay - day));
                  }else{
                      dateObj.last = year + '-' + (month - 3) + '-' + last3MonthDay;
                  }
              }else{
                  dateObj.last = year + '-' + (month - 3) + '-' + day;
              }
          }
          this.setData({
            'options[1].value':dateObj.last+'-'+dateObj.now
          })
        },
        // 获取近半年
        getLastHalfYear(){
          var now = new Date();
          var year = now.getFullYear();
          //0-11表示1-12月
          var month = now.getMonth() + 1;
          var day = now.getDate();
          var dateObj = {};
          dateObj.now = year + '-' + month + '-' + day;
          //当前月的总天数
          var nowMonthDay = new Date(year, month, 0).getDate();
          //如果是1、2、3,4,5,6月,年数往前推一年    
          if(month - 6 <= 0){ 
              //6个月前所在月的总天数
              var last3MonthDay = new Date((year - 1), (12 - (6 - parseInt(month))), 0).getDate();
              //6个月前所在月的总天数小于现在的天日期    
              if(last3MonthDay < day){    
                  dateObj.last = (year - 1) + '-' + (12 - (6 - month)) + '-' + last3MonthDay;
              }else{
                  dateObj.last = (year - 1) + '-' + (12 - (6 - month)) + '-' + day;
              }
          }else{
            //6个月前所在月的总天数
              var last3MonthDay = new Date(year, (parseInt(month) - 6), 0).getDate(); 
              //6个月前所在月的总天数小于现在的天日期   
              if(last3MonthDay < day){    
                 //当前天日期小于当前月总天数,2月份比较特殊的月份
                  if(day < nowMonthDay){       
                      dateObj.last = year + '-' + (month - 6) + '-' + (last3MonthDay - (nowMonthDay - day));
                  }else{
                      dateObj.last = year + '-' + (month - 6) + '-' + last3MonthDay;
                  }
              }else{
                  dateObj.last = year + '-' + (month - 6) + '-' + day;
              }
          }
          this.setData({
            'options[2].value':dateObj.last+'-'+dateObj.now
          })
        }
  • 相关阅读:
    Django路由系统
    修改数据库时区问题
    Django框架篇
    前端css
    前端html
    前端初识
    数据库3
    数据库2
    数据库1
    数据库初识
  • 原文地址:https://www.cnblogs.com/bm20131123/p/13902472.html
Copyright © 2011-2022 走看看