vue起始时间 终止时间 时间间隔

  • 页面

            起始日期:
            
            
          

          
            终止日期:
            
            
          

          
            时间间隔:
            


            时间单位:
            
              
              
            
          

  • 计算
productTarget() {
        if(this.startTime == null||this.startTime==''||typeof (this.startTime) == undefined){
          this.$message.error("请选择起始日期");
          return false;
        }
        if(this.endTime == null||this.endTime==''||typeof (this.endTime) == undefined){
          this.$message.error("请选择终止日期");
          return false;
        }
        var time1 = new Date(this.startTime).getTime();
        var time2 = new Date(this.endTime).getTime();

    
        var addSecond = 0;
        if(this.unitTime == ''){
          this.$message.error("请选择时间单位");
          return false;
        }
        if(this.unitTime == '分钟'){
          addSecond = 1000*60;
        }else if(this.unitTime == '时'){
          addSecond = 1000*60*60;
        }else if(this.unitTime == '日'){
          addSecond = 1000*60*60*24;
        }else if(this.unitTime == '月'){
          addSecond = 1000*60*60*24;
        }else{
          this.$message.error("请选择转换字段");
          return false;
        }

        if(this.intervalTime == null||this.intervalTime==''||typeof (this.intervalTime) == undefined){
          this.$message.error("请填写时间间隔");
          return false;
        }
        addSecond = addSecond * this.intervalTime;

        var sub = time2 - time1;
        if(addSecond>sub){
          this.$message.error("时间间隔大于时间短,请修改");
          return false;
        }
        var flag ;
        if(time1time2){
          flag = false;
        }
        if(!flag){
          this.$message.error("起始时间大于终止时间,请修改");
          return false;
        }

        this.targetField = [];
        var xuhao = true;
        var fieldType = this.dataFields.fieldType;
        if(this.targetTimeField == ''){
          this.$message.error("请选择转换格式:");
          return false;
        }else if(this.targetTimeField == '12'){//序号
          xuhao = true;
        }else{
          xuhao = false;
        }
        var startIndex = this.startIndex();
        var endIndex = this.endIndex();
        console.log("startIndex   "+JSON.stringify(startIndex))
        console.log("endIndex   "+JSON.stringify(endIndex))
        var target = 0;
        do {
          var newTime = this.fortime(time1);

          if(xuhao){ //序号从零排序
            target = target +1;
          }else {
            target = newTime.split("-").join("").split(" ").join("").split(":").join("");
            target = target.substring(startIndex,endIndex)
          }
          let param ={
            targetField:this.prefix+target,
            field:this.dataFields,
            keyValue:newTime,
            valueType:fieldType,
          }
          this.targetField.push(param)
          time1 = time1+addSecond;
        }while (time1<=time2)
        // console.log("this.targetField   "+JSON.stringify(this.targetField))
        return true;
      },
  • 引用函数
add(m){return m<10?'0'+m:m},
       fortime(shijianchuo){
        var time = new Date(shijianchuo);
        var y = time.getFullYear();
        var m = time.getMonth() + 1;
        var d = time.getDate();
        var h = time.getHours();
        var mm = time.getMinutes();
        var s = time.getSeconds();
        return y + '-' + this.add(m) + '-' + this.add(d) + ' ' + this.add(h) + ':' + this.add(mm) + ':' + this.add(s);
      },

你可能感兴趣的:(vue起始时间 终止时间 时间间隔)