在使用el-date-picker时,想要以下需求:
<el-date-picker
v-model="value"
type="daterange"
range-separator="到"
start-placeholder="开始时间"
end-placeholder="结束时间"
value-format="YYYY-MM-DD"
@change="change"
:disabled-date="disabledDate"
@calendar-change="handleChange"
@focus="handleFocus"
:clearable="false"
/>
const pickDay = ref()
const handleFocus = () => {
pickDay.value = null
}
const limitTime = 1000 * 60 * 60 * 24 * 6
const disabledDate = (time: Date) => {
if (pickDay.value) {
return (
time.getTime() < pickDay.value.getTime() - limitTime ||
time.getTime() > pickDay.value.getTime() + limitTime ||
time.getTime() > Date.now()
)
} else {
return time.getTime() > Date.now()
}
}
const handleChange = (val: Date[]) => {
const [pointDay] = val
pickDay.value = pointDay
}
或者使用dayJs,disabledDate
可以改为
import dayjs from 'dayjs'
const disabledDateSeven = (time: Date) => {
if (pickDay?.value) {
return (
time.getTime() < dayjs(pickDay.value.getTime()).subtract(6, 'day') ||
time.getTime() > dayjs(pickDay.value.getTime()).add(6, 'day') ||
time.getTime() > Date.now()
)
} else {
return time.getTime() > Date.now()
}
}