element的el-date-picker时间控件,限制选择范围区间天数并且当前之后的日期不可选
HTML部分代码
<el-date-picker
v-model="dateRange"
type="datetimerange"
value-format="yyyy-MM-dd HH:mm:ss"
range-separator="至"
start-placeholder="开始日期"
end-placeholder="结束日期"
:default-time="['00:00:00', '23:59:59']"
:picker-options="pickerOptions"
style="margin-right: 150px;"
>
</el-date-picker>
JS代码部分
data(){
return {
nextTime:'',
dateRange: [],
pickerMinDate: '',
pickerOptions: {
onPick: ({ maxDate, minDate }) => {
this.pickerMinDate = minDate.getTime()
if (maxDate) {
this.pickerMinDate = ''
}
},
disabledDate: (time) => {
let date=new Date();
let year=date.getFullYear();
let month=date.getMonth()+1;
let day=date.getDate();
let now=`${year}-${month}-${day} 23:59:59`;
let mynow=new Date(now).getTime()
if (this.pickerMinDate) {
let one = 9 * 24 * 3600 * 1000。
let minTime = this.pickerMinDate - one
let maxTime = this.pickerMinDate + one
return time.getTime() < minTime || time.getTime() > maxTime || time.getTime()>mynow
}else{
return time.getTime() > mynow
}
},
}
}
},
实际效果图(当前日期为20231204,所以当前日期以后的不可选)
代码使用需要看看我代码里面的注释,根据自己的需求更改一下!