antd RangePicker限制最大时间跨度31天
type RangeValue = [Moment | null, Moment | null] | null;
const {RangePicker} = DatePicker;
const dateFormat: string = 'YYYY-MM-DD';
const start = moment().subtract(7, 'day').startOf('day');
const end = moment().subtract(1, 'day').endOf('day');
<RangePicker
allowClear={false}
value={dates}
disabledDate={disabledDate}
onCalendarChange={val => setDates(val)}
onOpenChange={onOpenChange}
ormat={dateFormat}
/>
const [timeRange, setTimeRange] = useState<RangeValue>([start, end]);
const [dates, setDates] = useState<RangeValue>([start, end]);
const disabledDate = (current: Moment) => {
if (!dates) {
return false;
}
const tooLate = dates[0] && current.diff(dates[0], 'days') > 31;
const tooEarly = dates[1] && dates[1].diff(current, 'days') > 31;
const isAfter = current && moment(current).isAfter();
return !!tooEarly || !!tooLate || isAfter;
};
const onOpenChange = (open: boolean) => {
if (open) {
setDates([null, null]);
}
else if (dates?.filter(it => it)?.length < 2) {
setTimeRange([start, end]);
setDates([start, end]);
}
else if (dates?.filter(it => it)?.length === 2) {
setTimeRange(dates);
}
};