5

Working on antd framework, I am trying to disable the DatePicker date which are less than given defaultDate, I am not able to get it right by any means. The situation is say defaultDate of the Date Picker is 2028-12-20 all dates should be disabled below this..

The callback to do this is as follows

disabledDate = (current) => {
    return current && current < moment().endOf('day');;
}

where current = defaultDate which has been provided it doesn't change, I am not sure how to do this..

I have create a SandBox here

Any help would be highly appreciated.

Thanks.

iphonic
  • 12,615
  • 7
  • 60
  • 107
  • Check this please https://codesandbox.io/s/p34o4v7wlj , is that what you are looking for? – Eugene Dzhevadov Nov 23 '18 at 10:55
  • @EugeneDzhevadov If the currentDate or defaultDate is `Today` it works fine to me similar to , but if the defaultDate is something like in future as in my example I fail to achieve it. – iphonic Nov 23 '18 at 11:00
  • I've updated an example https://codesandbox.io/s/p34o4v7wlj – Eugene Dzhevadov Nov 23 '18 at 11:07
  • @EugeneDzhevadov It has disabled all the dates now.. – iphonic Nov 23 '18 at 11:10
  • So, `current` is passed from the component, my guess is that it is always today, but you can define a `default` value instead of `current` and do any logic more, less or equal, but I see the component display today date – Eugene Dzhevadov Nov 23 '18 at 11:14

2 Answers2

10

It can be done in following way:

disabledDate(current) {
  let customDate = "2018-11-25";
  return current && current < moment(customDate, "YYYY-MM-DD");
}

Here is the working demo

Triyugi Narayan Mani
  • 3,039
  • 8
  • 36
  • 56
4

In addition, you can disable date with enable your available date range / slot.

npm install date-fns

Package for add or sub days.

import {subDays, addDays } from 'date-fns'

Final code

<DatePicker 
disabledDate={d => !d || d.isAfter(addDays( new Date(), 7)) || d.isSameOrBefore(subDays( new Date(), 1)) }/>

Hope it's helpful.

Majedur
  • 3,074
  • 1
  • 30
  • 43