el-date-picker 前后七天
时间: 2024-06-02 12:06:09 浏览: 106
【JavaScript源代码】vue el-date-picker动态限制时间范围案例详解.docx
el-date-picker 是一个基于 Element UI 的日期选择器组件,可用于选择日期和时间。如果您需要选择前后七天的日期,可以设置 el-date-picker 的可选日期范围为当前日期加减七天。以下是实现方式:
1. 设置可选日期范围为当前日期加减七天:
```html
<el-date-picker :picker-options="pickerOptions"></el-date-picker>
```
```js
export default {
data() {
return {
pickerOptions: {
disabledDate(time) {
const weekAgo = new Date()
weekAgo.setTime(weekAgo.getTime() - 7 * 24 * 60 * 60 * 1000)
return time.getTime() < weekAgo.getTime() || time.getTime() > new Date().getTime()
}
}
}
}
}
```
2. 设置默认值为当前日期:
```html
<el-date-picker v-model="date" :picker-options="pickerOptions"></el-date-picker>
```
```js
export default {
data() {
return {
date: new Date(),
pickerOptions: {
disabledDate(time) {
const weekAgo = new Date()
weekAgo.setTime(weekAgo.getTime() - 7 * 24 * 60 * 60 * 1000)
return time.getTime() < weekAgo.getTime() || time.getTime() > new Date().getTime()
}
}
}
}
}
```
阅读全文