Cristo Ferrao
Cristo Ferrao

Reputation: 105

antd datepicker allow date 30 days before current and after current 30 days

working on react project using ant design datepicker in which I want pick date from current 30 days before and 30 days after and disable the rest of the dates

Anyone help

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}
 

/>

this code gives current and before that disabled its using moment

Please help

Upvotes: 0

Views: 3648

Answers (3)

bunny899
bunny899

Reputation: 11

You can do something like this

  <DatePicker format={'YYYY-MM-DD'}
            style={{ width: '100%' }}
            disabledDate={current =>
              !current ||
              current.isAfter(moment().add(30, 'days')) ||
              current.isSameOrBefore(moment().subtract(30, 'days'))
            }
          />

Hope it will help you cheers!

Upvotes: 1

Cristo Ferrao
Cristo Ferrao

Reputation: 105

Thanks but got it with this

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

Upvotes: 2

Dobromir Kirov
Dobromir Kirov

Reputation: 1033

I assume that the arg current is number and you are comparing it to moment().endOf('day') which is object.

Start from there, because for any number that you try to compare to this object will return true, e.g.

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

Upvotes: 1

Related Questions