element 日期选择器 el-date-picker 限制可选时间范围

template

	<el-date-picker
      v-model="value"
      type="daterange"
      :picker-options="pickerOptions"
      value-format="yyyy-MM-dd"
      range-separator="-"
      start-placeholder="开始日期"
      end-placeholder="结束日期">

data

	pickDate:'',// 存放getPickDate获取的数据
	pickerOptions: {
  		onPick: this.getPickDate,
  		disabledDate: this.disabledDate
	},
	pickerOptionsSecond: 0

created

	created() {
	  this.pickerOptionsSecond = 3600 * 1000 * 24 *365 // 限制可选一年
	},

methods

// 时间跨度限制
    getPickDate(pick) {
     this.pickDate = pick;
    },
    disabledDate(date) {
      if(!this.pickerOptionsSecond) {
        return false
      }
      const { minDate, maxDate } = this.pickDate;
      if (minDate && !maxDate) {
        const diff = Math.abs(minDate.valueOf() - date.valueOf());
        if (diff > this.pickerOptionsSecond) {
          return true;
        }
      }
    }

你可能感兴趣的:(前端,javascript,vue.js)