react-toolbox icon indicating copy to clipboard operation
react-toolbox copied to clipboard

Datepicker enabledDates & disabledDates not working

Open alengel opened this issue 9 years ago • 2 comments

Hi there, awesome work you're doing here!

On version 1.3.4, I'm trying to use the DatePicker enabledDates props to pass in an array of dates to the DatePicker that are supposed to be available to the user. In the process, I also used the disabledDates props because at first I wasn't quite clear if some of my logic is incorrect and I wanted to verify with both that they don't behave the way I expect.

Here's my code:

const TODAY = moment().toDate();

<DatePicker
  autoOk
  disabledDates={ this.getDisabledDates() }
  inputFormat={(value) => `${value.getDate()} ${moment(value).format('MMM')}, ${value.getFullYear()}`}
  label="Start Date"
  minDate={ TODAY }
  onChange={ this.handleChange.bind(this, 'start_date') }
  value={ this.state.start_date }
/>

The getDisabledDates fn looks like this:

getDisabledDates() {
    const firstDate = moment();
    const lastDate = moment().add(1, 'weeks');
    let day = firstDate;
    let dates = [];

    while (day <= lastDate) {
      dates.push(day.toDate());
      day = day.clone().add(1, 'days');
    }

    return dates;
  }

The dates array I'm returning looks like this:

[Mon Aug 21 2017 16:56:04 GMT+0200 (CEST), Tue Aug 22 2017 16:56:04 GMT+0200 (CEST), Wed Aug 23 2017 16:56:04 GMT+0200 (CEST), Thu Aug 24 2017 16:56:04 GMT+0200 (CEST), Fri Aug 25 2017 16:56:04 GMT+0200 (CEST), Sat Aug 26 2017 16:56:04 GMT+0200 (CEST), Sun Aug 27 2017 16:56:04 GMT+0200 (CEST), Mon Aug 28 2017 16:56:04 GMT+0200 (CEST)]

Checking an object in the console evaluates instanceof Date to true, so I believe that they are correct date objects. Any idea what might be wrong here?

alengel avatar Aug 21 '17 15:08 alengel

The date must match on the millisecond level so you must use dates with time 00:00:00 0000 (in the users timezone).

This should probably be changed though to include any time during the day.

Pajn avatar Aug 31 '17 17:08 Pajn

For anyone else having trouble with this, I was able to get it by creating a date object with just the year, month, and day.

new Date(year, month, day)

Adding more than that would break it for me.

dweirich avatar Oct 06 '17 05:10 dweirich