需求:默认展示最近30天
时间选择限制最大30天
pickerMinDate: '', // 保存选中的开始时间
pickerOptions1: {
onPick: obj => {
this.pickerMinDate = new Date(obj.minDate).getTime()
const start = this.formatDate(obj.minDate, 'start')
const end = this.formatDate(obj.maxDate, 'end')
obj.maxDate && (this.date = [start, end])
},
disabledDate: time => {
if (this.pickerMinDate) {
const oneMonth = 1000 * 3600 * 24 * 31
const maxTime = this.pickerMinDate + oneMonth
const mixTime = this.pickerMinDate - oneMonth + 1000 * 3600 * 24
const tomorrow = new Date(this.formatDate(new Date().getTime(), 'start'))
return time.getTime() >= tomorrow || time.getTime() >= maxTime || time.getTime() < mixTime
}
}
}
// 默认选择最近30天
formatDate(datetime, type) {
const time = new Date(datetime)
const year = time.getFullYear()
const month = (time.getMonth() + 1).toString().padStart(2, '0')
const date = (time.getDate()).toString().padStart(2, '0')
return type === 'start' ? year + '-' + month + '-' + date : year + '-' + month + '-' + date
},
getDate() {
const start = new Date()
this.$set(this.date, 0, this.formatDate(start.getTime() - 1000 * 3600 * 24 * 30, 'start'));
this.$set(this.date, 1, this.formatDate(start.getTime(), 'end'));
},
//失去焦点清空最小时间
pickBlur() {
this.pickerMinDate = ''
},
<el-date-picker :key="isLongPeriod" v-if="isLongPeriod" v-model="date" type="daterange"
:picker-options='pickerOptions1' range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期"
@blur="pickBlur">
</el-date-picker>