Reputation: 105
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
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
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
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