el-date-picker 限制30天日期选择范围

<el-date-picker  v-model="date" type="daterange" @blur="pickBlur"
   :picker-options='pickerOptions' range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期"
</el-date-picker>

data数据设置

      pickerMinDate: '', // 保存选中的开始时间
      pickerOptions: {
        onPick: obj => {
          this.pickerMinDate = new Date(obj.minDate).getTime()
          const start = this.formatDate(obj.minDate, 'start')
          const end = this.formatDate(obj.maxDate, 'end')
          obj.maxDate && (this.date = [start, end])
        },
        disabledDate: time => {
          if (this.pickerMinDate) {
            const oneMonth = 1000 * 3600 * 24 * 30 // 设置限制30天的日期
            const maxTime = this.pickerMinDate + oneMonth
            const mixTime = this.pickerMinDate - oneMonth + 1000 * 3600 * 24
            const tomorrow = new Date(this.formatDate(new Date().getTime(), 'start'))
            return time.getTime() >= tomorrow || time.getTime() >= maxTime || time.getTime() < mixTime
          }
        }
      }

methods方法

    // 默认选择最近30天
    formatDate(datetime, type) {
      const time = new Date(datetime)
      const year = time.getFullYear()
      const month = (time.getMonth() + 1).toString().padStart(2, '0')
      const date = (time.getDate()).toString().padStart(2, '0')
      return type === 'start' ? year + '-' + month + '-' + date : year + '-' + month + '-' + date
    },
    getDate() {
      const start = new Date()
      this.$set(this.date, 0, this.formatDate(start.getTime() - 1000 * 3600 * 24 * 30, 'start'));
      this.$set(this.date, 1, this.formatDate(start.getTime(), 'end'));
    },
//失去焦点清空最小时间
    pickBlur() {
      this.pickerMinDate = ''
    },

© 版权声明

相关文章

暂无评论

您必须登录才能参与评论!
立即登录
暂无评论...