日期选择组件,限制选择区间1年

以下方法适用daterange类型组件

1.跨度一年但不限制时间

pickerOptions: {
    disabledDate: (time) => {
	    const choiceDateTime = new Date(this.selectDate).getTime();
	    const minTime = new Date(choiceDateTime).setMonth(new Date(choiceDateTime).getMonth() - 12);
	    const maxTime = new Date(choiceDateTime).setMonth(new Date(choiceDateTime).getMonth() + 12);
	    if (this.selectDate) {
		    return time.getTime() < minTime || time.getTime() > maxTime;
	    }
	    return false;
	},
	onPick: ({ maxDate, minDate }) => {
		this.selectDate = minDate.getTime();
		if (maxDate) {
		    this.selectDate = '';
		}
	}
}

2.跨度一年但是最大不能超过明天

pickerOptions: {
	disabledDate: (time) => {
		const selectDateTime = new Date(this.selectDate).getTime();
		const minDate = new Date(selectDateTime).setMonth(new Date(selectDateTime).getMonth() - 12);
		const maxDate = new Date(selectDateTime).setMonth(new Date(selectDateTime).getMonth() + 12);
		const min = minDate;
		const newDate = new Date(new Date().toLocaleDateString()).getTime() + 24 * 60 * 60 * 1000 - 1;
		const max = newDate < maxDate ? newDate : maxDate;
		if (this.selectDate) {
			return time.getTime() < min || time.getTime() > max;
		}
		return time.getTime() > newDate;
	},
	onPick: ({ maxDate, minDate }) => {
		this.selectDate = minDate.getTime();
		if (maxDate) {
			this.selectDate = '';
		}
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值