element ui 开始时间和结束时间,时间限制,带年月日时分秒

如题:element ui 开始时间和结束时间,时间限制,带年月日时分秒

最终样子:

 

template部分:

<el-form-item label="时间" label-width="90px">
              <el-date-picker
                v-model="searchCond.startTime"
                type="datetime"
                placeholder="开始时间"
                :clearable="false"
                value-format="yyyy-MM-dd HH:mm:ss"
                :picker-options="pickerOptionsStart"
              />
              <span style="padding: 0 3px; color: #000"> 至 </span>
              <el-date-picker
                v-model="searchCond.endTime"
                type="datetime"
                :clearable="false"
                placeholder="结束时间"
                value-format="yyyy-MM-dd HH:mm:ss"
                :picker-options="pickerOptionsEnd"
              />
            </el-form-item>

data部分

//一个月
// 开始结束日期限制
      pickerOptionsStart: {
        selectableRange: '00:00:00 - 23:59:59',
        disabledDate: (time) => {
          if (this.searchCond.endTime) {
            return (
              time.getTime() > new Date(this.searchCond.endTime).getTime() ||
              time.getTime() <
                new Date(this.searchCond.endTime).getTime() -
                  1000 * 60 * 60 * 24 * 31
            )
          }
        }
      },
      // 结束日期限制
      pickerOptionsEnd: {
        selectableRange: '00:00:00 - 23:59:59',
        disabledDate: (time) => {
          if (this.searchCond.startTime) {
            return (
              time.getTime() < new Date(this.searchCond.startTime).getTime() ||
              time.getTime() >
                new Date(this.searchCond.startTime).getTime() +
                  1000 * 60 * 60 * 24 * 30
            )
          }
        }
      }

js部分(添加监听):

watch: {
    'searchCond.endTime': {
      handler(newValue, oldValue) {
        if (newValue) {
          let timeST = ''
          let st = ''
          if (this.searchCond.startTime) {
            const date = new Date(this.searchCond.startTime)
            //此处用到moment插件,可自行安装此插件或者类似的插件即可
            const nowDate = this.$moment(date).format('HH:mm:ss')
            if (
              this.$moment(date).format('yyyy-MM-DD') ===
              this.$moment(newValue).format('yyyy-MM-DD')
            ) {
              const hh1 = this.$moment(newValue).format('HH:mm:ss')
              if (hh1 < nowDate) {
                this.searchCond.endTime = new Date(this.searchCond.startTime)
              }
              st = nowDate
              timeST = nowDate
            } else {
              st = '00:00:00'
              timeST = '23:59:59'
            }
          } else {
            st = '00:00:00'
            timeST = '23:59:59'
          }
          this.pickerOptionsStart.selectableRange = '00:00:00 - ' + timeST
          this.pickerOptionsStart = this.pickerOptionsStart
          this.pickerOptionsEnd.selectableRange = st + ' - 23:59:59'
          this.pickerOptionsEnd = this.pickerOptionsEnd
        }
      },
      deep: true,
      immediate: true
    },
    'searchCond.startTime': {
      handler(newValue, oldValue) {
        if (newValue) {
          let timeST = ''
          let st = ''
          if (this.searchCond.endTime) {
            const date = new Date(this.searchCond.endTime)
            const nowDate = this.$moment(date).format('HH:mm:ss')
            if (
              this.$moment(date).format('yyyy-MM-DD') ===
              this.$moment(newValue).format('yyyy-MM-DD')
            ) {
              const hh1 = this.$moment(newValue).format('HH:mm:ss')
              if (hh1 > nowDate) {
                this.searchCond.startTime = new Date(this.searchCond.endTime)
              }
              st = nowDate
              timeST = nowDate
            } else {
              st = '00:00:00'
              timeST = '23:59:59'
            }
          } else {
            st = '00:00:00'
            timeST = '23:59:59'
          }
          this.pickerOptionsStart.selectableRange = '00:00:00 - ' + timeST
          this.pickerOptionsStart = this.pickerOptionsStart
          this.pickerOptionsEnd.selectableRange = st + ' - 23:59:59'
          this.pickerOptionsEnd = this.pickerOptionsEnd
        }
      },
      deep: true,
      immediate: true
    }
  },

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值