Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to disable specific dates in the date picker using the props shouldDisableDates, but as it accepts only functions I have written a function but it is always return false.

What I have tried:

code
<datepicker
="" autook="{true}
" format="YYYY-MM-DD" shoulddisbaledate="{()" ==""> disbaleDates()}
onChange={e => setDate(e)
/>

const disableDates = () => {
let date = ['2021-01-1', '2021-02-01']
const dateInt = [
new Date(date[0]),
new Date(date[1]),
]
return dateInt.indluces(new Date(date)) // this is return false where in out case has to return true
}
Posted
Updated 12-Jan-21 19:02pm

1 solution

Quote:
return dateInt.indluces(new Date(date))

Spelling of includes look incorrect
Refer: JavaScript Array includes() Method[^]

Quote:
shouldDisableDate (day: DateIOType) => boolean => Disable specific date


You can add like:
JavaScript
import React from 'react';
import DatePicker from 'material-ui/DatePicker';

function disableWeekends(date) {
  return date.getDay() === 0 || date.getDay() === 6;
}

function disableRandomDates() {
  return Math.random() > 0.7;
}
/**
 * `DatePicker` can disable specific dates based on the return value of a callback.
 */
const DatePickerExampleDisableDates = () => (
  <div>
    <DatePicker hintText="Weekends Disabled" shouldDisableDate={disableWeekends} />
    <DatePicker hintText="Random Dates Disabled" shouldDisableDate={disableRandomDates} />
  </div>
);

export default DatePickerExampleDisableDates;
 
Share this answer
 
Comments
sree Harish 13-Jan-21 1:06am    
@SandeepMewara that would disable random dates but I want to disable specific dates
Sandeep Mewara 13-Jan-21 1:19am    
That was an example. You can put speciffic dates in that method like:

disableSpecific(datef) {
const dateRaw = [
new Date(date.getFullYear(),0,1),
new Date(date.getFullYear(),4,1)
];

return dateRaw.includes(datef.getTime());
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900