reactjsantd

ReactTS, antd, DataPicker. How to disable dates except the custom list?


How to disable all dates in the calendar except my custom list?

    const disabledDates = ["2025-27-06", "2025-30-06", "2025-05-07", "2025-10-07"];

    function disabledDateFunc(current: object) {          
         ...
    }


            <Form.Item<DoctorSheduleRequest>
                label="To select day"
                name="day"
                rules={[{ required: true, message: 'Please input startday!' }]}
            >
                <DatePicker
                    disabledDate={disabledDateFunc}
                    onChange={selectDate}
                />
            </Form.Item>

The dates in the list are not consecutive!


Solution

  • You can enable only your custom dates in Ant Design's DatePicker by using dayjs like this:

    import dayjs, { Dayjs } from 'dayjs';
    
    const allowedDates = ['2025-27-06', '2025-30-06', '2025-05-07', '2025-10-07'].map(date => 
      dayjs(date, 'YYYY-DD-MM')
    );
    
    function disabledDateFunc(current: Dayjs): boolean {
      return current && !allowedDates.some(allowed => allowed.isSame(current, 'day'));
    }
    

    Add it to your DatePicker:

    <DatePicker disabledDate={disabledDateFunc} onChange={selectDate} />
    

    Hope it helps!