需求:
1.先开始日期,只能选大于当前日期,结束日期不能选开始日期这天,并小于前日期的后七天
2.如果先选结束日期,要求结束日期可选为当前日期之后的日期,再选开始日期,开始日期不能选结束日期的那天,并开始日间不能大于结束日期,而要在当天日期的有效内
代码如下:
<el-row>
<el-col :span="12">
<el-form-item label="布控起始时间" prop="bkqssj">
<el-date-picker type="date" placeholder="请选择日期" v-model="search.bkqssj" class="label-data" value-format="yyyy-MM-dd" :picker-options="gjOptionsStart"></el-date-picker>
</el-form-item>
</el-col>
<el-col :span="12">
<el-form-item label="布控截止时间" prop="bkjzsj">
<el-date-picker type="date" placeholder="请选择日期" v-model="search.bkjzsj" class="label-data" value-format="yyyy-MM-dd" :picker-options="gjOptionsEnd"></el-date-picker>
</el-form-item>
</el-col>
</el-row>
js
gjOptionsStart: {
disabledDate: (time) => {
if (this.search.bkjzsj) { //如果有开始时间
const dateScope = 7 * 24 * 3600 * 1000
const end = new Date(this.search.bkjzsj)
const date = new Date(end).getTime()
return (
// 返回小于7天的,或都返回当前时间减去7天有效期内的
(new Date(this.search.bkjzsj).getTime() - 8.64e7) < time.getTime() || time.getTime() < Date.now() - 8.64e7 || time.getTime() + 8.64e7 < date - dateScope
)
}
return time.getTime() < Date.now() - 8.64e7
}
},
gjOptionsEnd: {
disabledDate: (time) => {
if (this.search.bkqssj) {
return new Date(this.search.bkqssj).getTime() > time.getTime() || new Date(this.search.bkqssj).getTime() < time.getTime() - 1000 * 3600 * 24 * 7
}
else {
return time.getTime() < Date.now()
}
}
},