1. 程式人生 > 其它 >a-date-picker 限制選擇當前時間以後的時間

a-date-picker 限制選擇當前時間以後的時間

disabledDate(current) {
      const time = moment().endOf('day').subtract(1, 'days')
      return current && current < time
    },
    rangeHours(start, end, value, chooseday, day) {
      const result = []
      if (chooseday === day) {
        for (let i = start; i < end; i++) {
          if (i < value) {
            result.push(i)
          }
        }
      }
      return result
    },
    rangeMinutes(start, end, value, chooseday, day, chooseh, h) {
      const result = []
      if (chooseday === day && chooseh === h) {
        for (let i = start; i < end; i++) {
          if (i < value) {
            result.push(i)
          }
        }
      }
      return result
    },
    rangeSeconds(start, end, value, chooseday, day, chooseh, h, choosem, m) {
      const result = []
      if (chooseday === day && chooseh === h && choosem === m) {
        for (let i = start; i < end; i++) {
          if (i < value) {
            result.push(i)
          }
        }
      }
      return result
    },
    disabledDateTime(date) {
      // 選中的時間
      const choosetime = moment(date).format('HH-mm-ss')
      const chooseday = parseInt(moment(date).format('YYYY-MM-DD').split('-')[2])
      const chooseh = parseInt(choosetime.split('-')[0])
      const choosem = parseInt(choosetime.split('-')[1])
      // 當前時間
      const nowtime = moment().format('HH-mm-ss')
      const day = parseInt(moment().format('YYYY-MM-DD').split('-')[2])
      const h = parseInt(nowtime.split('-')[0])
      const m = parseInt(nowtime.split('-')[1])
      const s = parseInt(nowtime.split('-')[2])
      return {
        disabledHours: () => this.rangeHours(0, 24, h, chooseday, day),
        disabledMinutes: () => this.rangeMinutes(0, 60, m, chooseday, day, chooseh, h),
        disabledSeconds: () => this.rangeSeconds(0, 60, s, chooseday, day, chooseh, h, choosem, m)
      }
    },

  

<a-col :span="12">
                <a-form-item :labelCol="labelCol"
                             :wrapperCol="wrapperCol"
                             label="票面出發時間">
                  <a-date-picker style="width: 100%"
                                 show-time
                                 :disabled
-date="disabledDate" :disabled-time="disabledDateTime" format="YYYY-MM-DD HH:mm:ss" placeholder="請選擇票面出發時間" v-decorator="['buyticketDepartureTime', {rules: [{required: true, message: '請選擇票面出發時間'}]}]" /> </a-form-item> </a-col>