1,659

antd的RangePicker日期选择时间限定区间最大为1年,不限制选择时间,但是前后时间相差不能超过一年

import React, {  useState, useCallback } from 'react';
import moment from 'moment';
import { DatePicker } from 'antd';
const { RangePicker } = DatePicker;
function (){
 const [initDisableTime, setInitDisableTime] = useState([]); // 设置时间范围不可选的初始值
 // 设置时间不可选范围
  const CalendarChange = (dates) => {
    if (dates.length === 1) {
      setInitDisableTime(dates[0]);
  const openChange = () => {
    setInitDisableTime('');
   // 发起时间的选择区间1年前-今日
  const disabledDate = useCallback(
    (currentDay) => {
      return (
        currentDay < moment(initDisableTime).subtract(1, 'year') ||
        currentDay > moment(initDisableTime).add(1, 'year').add(1, 'day')
    [initDisableTime]
 return 
 <RangePick 
 placeholder="请选择"
 disabledDate={disabledDate} 
 format="YYYY-MM-DD" 
 onCalendarChange={CalendarChange} 
 onOpenChange={openChange} />
    做个快乐的小麦麦