需求:时间选择器 只能选择7天及以内
<template>
<el-date-picker
v-model="valueDate"
type="daterange"
range-separator="⇀"
start-placeholder="开始日期"
end-placeholder="结束日期"
format="YYYY-MM-DD"
value-format="YYYY-MM-DD"
size="large"
:disabled-date="disabledDate"
@calendar-change="calendarChange"
/>
</template>
<script lang="ts" setup>
import { ref } from "vue"
const valueDate = ref("")
const firstChooseDate = ref("")
const disabledDate = (time: Date) => {
if (firstChooseDate.value) {
const timeRange = 1 * 24 * 60 * 60 * 1000
const minTime = firstChooseDate.value - timeRange * 7
const maxTime = firstChooseDate.value + timeRange * 6
return time.getTime() <= minTime || time.getTime() > maxTime
} else {
return false
}
}
const calendarChange = (val: any) => {
firstChooseDate.value = val[0].getTime()
if (val[1]) firstChooseDate.value = ""
}
</script>
<style scoped></style>