vue element uI日期选择器时间选择范围限制

  • Post author:
  • Post category:vue


<el-date-picker
   v-model="time"
    type="month"
    value-format="yyyy-MM"
    range-separator="至"
    start-placeholder="开始月份"
    end-placeholder="结束月份"
    :picker-options="pickerOptions"
  />


大于当前日期不可选

 time: [],
 pickerOptions: {
    disabledDate(time) {
      return time.getTime() > Date.now() - 8.64e6
    }
 },


小于当前日期不可选

 time: [],
 pickerOptions: {
    disabledDate(time) {
      return time.getTime() > Date.now() - 8.64e6
    }
 },


设置区间

  time: [],
  pickerMinDate: '',
  pickerOptions: {
    onPick: ({ maxDate, minDate }) => {
      this.pickerMinDate = minDate.getTime()
      if (maxDate) {
        this.pickerMinDate = ''
      }
    },
    disabledDate: time => {
      if (this.pickerMinDate !== '') {
        // 改变7即可,这里限制一周
        const day30 = (7 - 1) * 24 * 3600 * 1000
        let maxTime = this.pickerMinDate + day30
        const minTime = this.pickerMinDate - day30
        if (maxTime > new Date()) {
          maxTime = new Date()
        }
        return time.getTime() > maxTime || time.getTime() < minTime
      }
      return time.getTime() > Date.now()
    }
  },


设置区间并且当前日期不可选

  time: [],
  pickerMinDate: '',
  pickerOptions: {
    onPick: ({ maxDate, minDate }) => {
      this.pickerMinDate = minDate.getTime()
      if (maxDate) {
        this.pickerMinDate = ''
      }
    },
    disabledDate: time => {
      if (this.pickerMinDate !== '') {
        // 改变7即可,这里限制一周
        const day30 = 7 * 24 * 3600 * 1000
        let maxTime = this.pickerMinDate + day30
        const minTime = this.pickerMinDate - day30
        if (maxTime > new Date()) {
          maxTime = new Date()
        }
        return time.getTime() + 8.64e7 > maxTime || time.getTime() - 8.64e7 < minTime
      }
      return time.getTime() > Date.now() - 8.64e7
    }
  },



版权声明:本文为qq_36441761原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。