antdv日期选择组件提供了一些属性,可以限制选择的日期范围。

可以使用disabledDate属性来禁用一些日期,例如:

<DatePicker disabledDate={disabledDate} />

其中disabledDate是一个函数,返回true表示禁用该日期,返回false表示可选择该日期。

以下是一个示例,禁用日期范围为当前日期前后7天:

import React from 'react';
import { DatePicker } from 'antd';

function disabledDate(current) {
  // 获取当前日期
  const currentDate = new Date();
  // 获取7天前的日期
  const lastWeek = new Date();
  lastWeek.setDate(currentDate.getDate() - 7);
  // 获取7天后的日期
  const nextWeek = new Date();
  nextWeek.setDate(currentDate.getDate() + 7);

  // 禁用日期范围为当前日期前后7天
  return current && (current.valueOf() < lastWeek || current.valueOf() > nextWeek);
}

function App() {
  return (
    <div>
      <DatePicker disabledDate={disabledDate} />
    </div>
  );
}

export default App;

通过以上代码,可以实现日期选择组件只能选择当前日期前后7天的日期

antdv 日期选择日期前后

原文地址: http://www.cveoy.top/t/topic/i1Bh 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录