element的el-date-picker时间控件,限制选择范围区间天数并且当前之后的日期不可选
HTML部分代码
javascript
<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代码部分
javascript
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() // 这一步很关键,我看很多博主发的方法直接是获取的new Date().getTime()这个时间,实际使用并不能满足需求,所以这里需要传入我上面定义的now这个变量值时间
if (this.pickerMinDate) {
let one = 9 * 24 * 3600 * 1000。// 我这里定义的是10天的选择范围区间,这个可以根据实际项目需求更改这个时间范围!
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
}
},
}
}
},