【问题标题】:antd datepicker allow date 30 days before current and after current 30 daysantd datepicker 允许日期在当前 30 天之前和当前 30 天之后
【发布时间】:2022-01-13 20:10:32
【问题描述】:

使用 ant design datepicker 处理 react 项目,我想从当前 30 天之前和 30 天之后选择日期并禁用其余日期

谁来帮忙

function disabledDate(current) {
  // Can not select days before today and today
  return current+30 && current < moment().endOf('day') ;
}



    <DatePicker
  format="YYYY-MM-DD HH:mm:ss"
  disabledDate={disabledDate}
 

/>

这段代码给出了当前和之前的禁用 它的使用时刻

请帮忙

【问题讨论】:

    标签: reactjs datepicker antd


    【解决方案1】:

    我假设 arg current 是数字,您将其与 moment().endOf('day') 进行比较,即 object

    从那里开始,因为您尝试与此 object 比较的任何数字都将返回 true,例如

    -1 < moment().endOf("day") => true
    0 < moment().endOf("day") => true
    1 < moment().endOf("day") => true
    

    【讨论】:

      【解决方案2】:

      谢谢,但是用这个得到了它

               <DatePicker
                  className="m-0 p-0 pr-2 pl-2  "
                  disabledDate={(current) => {                 
                    return (
                      moment().add(-1, "month") >= current ||
                      moment().add(1, "month") <= current
                    );
                  }}
               />
      

      【讨论】:

      • 是的,我就是这么想的,干杯!
      猜你喜欢
      • 2012-02-09
      • 1970-01-01
      • 2016-05-17
      • 2021-08-25
      • 1970-01-01
      • 2019-10-19
      • 2022-01-02
      • 2017-12-03
      • 2023-04-10
      相关资源
      最近更新 更多