ElementUI 多个时间选择器判断时间不能超过七天

时间选择器

<div class="block">
          <el-date-picker
            v-model="beanData"
            type="daterange"
            format="yyyy/MM/dd"
            @change="change"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
           >
          </el-date-picker>

data()

   change(data) {
        this.choiceDate=''
        let oDate1 = data[0]
        let oDate2 = data[1]
        let nowDate = new Date();
        if(oDate1 && oDate2) {
          if (oDate1.getTime() > oDate2.getTime()-1) {//开始时间不能大于等于结束时间
            this.$message({
              message: '开始时间不能大于等于结束时间',
              type: 'warning'
            })
            return;
          }

          if (oDate1.getTime() >= nowDate.getTime()) {//开始时间不能大于等于当前时间前一天
            this.$message({
              message: '开始时间不能大于等于当前时间前一天',
              type: 'warning'
            })
            return;
          }

          if (oDate2.getTime() > nowDate.getTime()-1000*60*60*24) {//结束时间必须小于当前时间前一天
            this.$message({
              message: '结束时间必须小于当前时间前一天',
              type: 'warning'
            })
            return;
          }

          if (oDate1.getTime() <= oDate2.getTime()-7*24*3600*1000) {//结束时间只能在开始时间后六天
            this.$message({
              message: '结束时间只能在开始时间后六天',
              type: 'warning'
            })
            return this.beanData[1]=new Date(oDate1.getTime()+6*24*3600*1000);
          }

        }

      },

这里判断的是开始时间不能大于结束时间 验证大于一秒

oDate1.getTime() > oDate2.getTime()-1

这里判断的是开始时间不能大于结束时间 验证大于一天

oDate2.getTime() > nowDate.getTime()-1000*60*60*24

这里判断的是开始时间不能大于结束时间 验证大于7天就给结束时间赋值,因为这里判读的是getTime,赋值的时候要转换成Date类型

    if (oDate1.getTime() <= oDate2.getTime()-7*24*3600*1000) {//结束时间不能大于当前时间
            this.$message({
              message: '结束时间只能在开始时间后六天',
              type: 'warning'
            })
            return this.beanData[1]=new Date(oDate1.getTime()+6*24*3600*1000);
          }

效果图 具体验证看个人需求
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值