antd限制时间选择区间 年月日时

1、html

     <div>
          <span>开始日期:</span>
          <a-date-picker
            v-model="startDate"
            :show-time="{ format: 'HH'}"
            format="YYYY-MM-DD HH"
            @change="onChangeStart"
            :disabled-date="disabledDateStart"
            :disabled-time="disabledTimeStart"/>
          <span style="margin-left:20px">结束日期:</span>
          <a-date-picker
            v-model="endDate"
            :show-time="{ format: 'HH'}"
            format="YYYY-MM-DD HH"
            @change="onChangeEnd"
            :disabled-date="disabledDateEnd"
            :disabled-time="disabledTimeEnd"/>
          <a-button class="editable-list-btn" type="primary" @click="search">{{ btnText }}</a-button>
        </div>

2、重点 js 限制年月日 时

在script的data里
 // 开始时间
      startDate: undefined,
      // 结束时间
      endDate: undefined,
      start: '',
      end: '',
      // 用来记录选择的开始时间
      timeStart: '',
      // 用来记录选择的结束时间
      timeEnd: '',
      // 开始的小时
      starthour: '',
      // 结束的小时
      endhour: '',
在script的methods里
   // 选择开始日期
    onChangeStart (e) {
      this.starthour = e.hour()
      this.timeStart = e
      this.startDate = moment(moment(e).format('YYYY-MM-DD HH'))
      this.start = moment(e).format('YYYY-MM-DD HH')
    },
    // 开始时间区间
    disabledDateStart (current) {
      if (this.timeEnd === '') {
        return current >= moment().endOf('day')
      }
      if (this.timeEnd !== '') {
        return current >= this.timeEnd.endOf('day') || current >= moment().endOf('day')
      }
      // return current <= this.timeEnd.endOf('day') || current <= this.timeEnd.add(-1, 'months')
    },
    // 开始日期限制小时
    disabledTimeStart (dateTime) {
      return {
        disabledHours: () => {
          if (this.timeEnd !== '') {
            if (this.timeEnd.isSame(dateTime, 'days')) {
              const hours = []
              for (var i = this.endhour; i < 24; i++) {
                hours.push(i)
              }
              return hours
            }
          }
          return []
        }
      }
    },
    // 结束日期限制小时
    disabledTimeEnd (date) {
      return {
        disabledHours: () => {
          if (this.timeStart !== '') {
            if (this.timeStart.isSame(date, 'days')) {
              // eslint-disable-next-line no-undef
              return _.times(this.starthour + 1)
            }
          }
          return []
        }
      }
    },
    // 选择结束时间
    onChangeEnd (e) {
      this.endhour = e.hour()
      this.timeEnd = e
      this.endDate = moment(moment(e).format('YYYY-MM-DD HH'))
      this.end = moment(e).format('YYYY-MM-DD HH')
    },
    // 结束时间区间
    disabledDateEnd (current) {
      if (this.timeStart === '') {
        // eslint-disable-next-line no-useless-return
        return current >= moment().endOf('day')
      }
      if (this.timeStart !== '') {
        return current <= this.timeStart.startOf('day') || current >= moment().endOf('day')
      }
    },
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值