<el-date-picker
          v-model="timeValue"
          style="margin-top:10px"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          format="yyyy 年 MM 月 dd 日"
          value-format="yyyy-MM-dd"
          :picker-options="pickerOptions"
          @change="dateSelectChange">
    </el-date-picker>

主要是:picker-options="pickerOptions"这个东西绑定

然后写规则

pickerOptions: {
        onPick: ({ maxDate, minDate }) => {
          this.cuttentTime = minDate.getTime()
          if (maxDate) {
            this.cuttentTime = ''
          }
        },
        disabledDate: (time) => {
          if (this.cuttentTime !== '') {
            const one = 30 * 24 * 3600 * 1000
            const minTime = this.cuttentTime - one
            const maxTime = this.cuttentTime + one
            return time.getTime() < minTime || time.getTime() > maxTime
          }
        }
      }

然后发还有问题,不是我想要的效果,我要的效果是限制只能一个月不能跨月,不要按天数算的那种,所以我改了代码

pickerOptions: {
        // 设置不能选择的日期
        onPick: ({ maxDate, minDate }) => {
          this.choiceDate = minDate.getTime()
          if (maxDate) {
            this.choiceDate = ''
          }
        },
        disabledDate: (time) => {
          const self = this
          if (self.choiceDate) {
            const startDay = (new Date(self.choiceDate).getDate() - 1) * 24 * 3600 * 1000
            const endDay = (new Date(new Date(self.choiceDate).getFullYear(), new Date(self.choiceDate).getMonth() + 1, 0).getDate() - new Date(self.choiceDate).getDate()) * 24 * 3600 * 1000
            const minTime = self.choiceDate - startDay
            const maxTime = self.choiceDate + endDay
            return time.getTime() < minTime || time.getTime() > maxTime
          }
        }
      },

 

问题解决!!希望对大家有帮助!! 

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐