需求:vue 基于element 时间控件,选择范围做控制,如下图
转载地址: https://blog.csdn.net/juse__we/article/details/80496980#commentBox
disabled的值是个布尔值,就是判断选择值是否为禁止日期(生成日历的时候也会触发一次判断)
组件代码:
<el-date-picker
v-model="value1"
type="date"
placeholder="选择日期"
:picker-options="pickerOptions0">
</el-date-picker>
情景1: 设置选择今天以及今天之后的日期
data (){
return {
pickerOptions0: {
disabledDate(time) {
return time.getTime() < Date.now()-8.64e7;
}
},
}
}
情景2: 设置选择今天以及今天以前的日期
data (){
return {
pickerOptions0: {
disabledDate(time) {
return time.getTime() > Date.now();
}
},
}
}
情景3: 设置选择今天之后的日期(不能选择当天时间)
data (){
return {
pickerOptions0: {
disabledDate(time) {
return time.getTime() < Date.now();
}
},
}
}
情景4: 设置选择今天之前的日期(不能选择当天)
data (){
return {
pickerOptions0: {
disabledDate(time) {
return time.getTime() > Date.now()-8.64e7;
}
},
}
}
- 择三个月之前到今天的日期
data (){
return {
pickerOptions0: {
disabledDate(time) {
let curDate = (new Date()).getTime();
let three = 90 * 24 * 3600 * 1000;
let threeMonths = curDate - three;
return time.getTime() > Date.now() || time.getTime() < threeMonths;;
}
},
}
}
情景6: 设置从4.12日开始,到今天 的前一天结束
data (){
return {
pickerOptions0: {
let Y = (new Date()).getFullYear();
let M = (new Date()).getMonth();
let D = (new Date()).getDate();
let curDate = (new Date()).getTime();
let defDate = (new Date(Y,M,D)).getTime() - new Date(2019,3,11).getTime(); // 14 * 24 * 3600 * 1000;因为月份取出来的值小1,所以直接用了new Date(2019,3,11).getTime()
let chooseDate = curDate - defDate;
return time.getTime() > Date.now()-8.64e7 || time.getTime() < chooseDate;
},
}
}
两个输入框—组件代码:
<el-date-picker
v-model="value1"
type="date"
placeholder="开始日期"
:picker-options="pickerOptions0">
</el-date-picker>
<el-date-picker
v-model="value2"
type="date"
placeholder="结束日期"
:picker-options="pickerOptions1">
</el-date-picker>
情景1: 限制结束日期不能大于开始日期
data(){
return {
pickerOptions0: {
disabledDate: (time) => {
if (this.value2 != "") {
return time.getTime() > Date.now() || time.getTime() > this.value2;
} else {
return time.getTime() > Date.now();
}
}
},
pickerOptions1: {
disabledDate: (time) => {
return time.getTime() < this.value1 || time.getTime() > Date.now();
}
},
}
}
转载地址:element时间范围选择添加限制条件_juse__we的博客-CSDN博客_element 时间选择限制