当前位置: 代码迷 >> 综合 >> el-date-picker 日期选择限制
  详细解决方案

el-date-picker 日期选择限制

热度:28   发布时间:2023-12-23 01:02:41.0

有需求要求选择日期时只能选择今天及明天的日期,其他日期禁止选择,方法如下:

只选择今天和明天日期:

<el-date-picker type="date" placeholder="选择日期" v-model="model.date1" :picker-options="pickerOptions" style="width: 230px;">
</el-date-picker>
data(){return{pickerOptions: {disabledDate(time) {let curDate = (new Date()).getTime();let oneDay = 1 * 24 * 3600 * 1000;let threeMonths = curDate + oneDay;return  time.getTime() < Date.now() - 8.64e7 || time.getTime() > threeMonths;}},}
}

效果如下:

只选择今天和昨天日期:

data(){return{pickerOptions: {disabledDate(time) {let curDate = (new Date()).getTime();let oneDay = 2 * 24 * 3600 * 1000;let threeMonths = curDate - oneDay;return time.getTime() > Date.now() || time.getTime() < threeMonths;}},}
}

效果如下:

只选择昨天今天和明天日期:

data(){return{pickerOptions: {disabledDate(time) {let curDate = (new Date()).getTime(); // 今天日期let oneDay = 2 * 24 * 3600 * 1000;let threeMonths = curDate - oneDay; // 昨天日期let oneDay2 = 1 * 24 * 3600 * 1000;let threeMonths2 = curDate + oneDay2; // 明天日期return time.getTime() < threeMonths || time.getTime() > threeMonths2;}},}
}

效果如下:

 

参考地址:【ElementUI】日期选择器时间选择范围限制 - ipCoder - 博客园

  相关解决方案