javascript
<el-date-picker v-model="query.startTime" type="datetime" :picker-options="startPickerOptions" format="yyyy-MM-dd HH时" popper-class="date-picker" placeholder="选择日期时间"></el-date-picker>
<el-date-picker v-model="query.endTime" type="datetime" :picker-options="endPickerOptions" format="yyyy-MM-dd HH时" popper-class="date-picker" placeholder="选择日期时间"> </el-date-picker>
javascript
data() {
return {
query: {
startTime: "",
endTime: "",
},
startPickerOptions: {
disabledDate: (time) => {
if (this.query.endTime) {
// 根据结束时间,向前推5天
let timeStart = getRequiresTime(
new Date(this.query.endTime),
"day",
-5
);
this.startPickerOptions.selectableRange = "02:00:00 - 23:59:59";
return (
time.getTime() > new Date(this.query.endTime).getTime() ||
time.getTime() < new Date(timeStart).getTime()
);
}
},
selectableRange: "00:00:00 - 23:59:59",
},
endPickerOptions: {
disabledDate: (time) => {
if (this.query.startTime) {
// 根据开始时间,向后推5天
let timeEnd = getRequiresTime(
new Date(this.query.startTime),
"day",
5
);
this.endPickerOptions.selectableRange = "04:00:00 - 23:59:59";
return (
new Date(this.query.startTime).getTime() > time.getTime() ||
time.getTime() > new Date(timeEnd).getTime()
);
}
},
selectableRange: "00:00:00 - 23:59:59",
},
}
}
javascript
// 半年,1年,3年前后的时间获取
export function getRequiresTime(time, type = null, number = 0) {
var nowdate = new Date(time);
switch (type) {
case "day": //取number天前、后的时间
nowdate.setTime(nowdate.getTime() + (24 * 3600 * 1000) * number);
var y = nowdate.getFullYear();
var m = nowdate.getMonth() + 1;
var d = nowdate.getDate();
var retrundate = y + '-' + (m >= 10 ? m : '0' + m) + '-' + (d >= 10 ? d : '0' + d);
break;
case "week": //取number周前、后的时间
var weekdate = new Date(nowdate + (7 * 24 * 3600 * 1000) * number);
var y = weekdate.getFullYear();
var m = weekdate.getMonth() + 1;
var d = weekdate.getDate();
var retrundate = y + '-' + (m >= 10 ? m : '0' + m) + '-' + (d >= 10 ? d : '0' + d);
break;
case "month": //取number月前、后的时间
nowdate.setMonth(nowdate.getMonth() + number);
var y = nowdate.getFullYear();
var m = nowdate.getMonth() + 1;
var d = nowdate.getDate();
var retrundate = y + '-' + (m >= 10 ? m : '0' + m) + '-' + (d >= 10 ? d : '0' + d);
break;
case "year": //取number年前、后的时间
nowdate.setFullYear(nowdate.getFullYear() + number);
var y = nowdate.getFullYear();
var m = nowdate.getMonth() + 1;
var d = nowdate.getDate();
var retrundate = y + '-' + (m >= 10 ? m : '0' + m) + '-' + (d >= 10 ? d : '0' + d);
break;
default: //取当前时间
var y = nowdate.getFullYear();
var m = nowdate.getMonth() + 1;
var d = nowdate.getDate();
var retrundate = y + '-' + (m >= 10 ? m : '0' + m) + '-' + (d >= 10 ? d : '0' + d);
}
return retrundate;
}