0

Does anyone know how I can pass an array of dates to react-datetime? I'm trying to implement a datepicker which can display "booked" dates from my state but currently I only managed to disable all days before the current one. So how can I pass the dates in my state to <Datetime/>, in order to disable them?

 'use strict';

import React       from 'react';
import {PropTypes} from 'prop-types';
import Datetime    from 'react-datetime';

class DatePicker extends React.Component {
    constructor(props) {
        super(props);

        this.state = {
            dates: [{id: 1, date: 'Apr 30 2020 09:00:00 AM'}, {id: 2, date: 'May 1 2020 12:00:00 PM'}]
        };

    }

    render() {
        const {t} = this.props;

        let yesterday = Datetime.moment().subtract(1, 'day');

        let valid = function (current) {
            return current.isAfter(yesterday);
        };

        return (
            <div className="date-picker">
                <p>{t('Date Picker')}</p>

                <Datetime timeFormat={false} isValidDate={valid(yesterday)}/>

            </div>
        );
    }
}

export default DatePicker;

1 Answer 1

1

Well first of all somethings wrong with your date format in state. Try setting up your dates using moment

this.state = {
   dates: [{
    id: 1,
    date: Datetime.moment("30 Apr 2020")
  },
  {
    id: 2,
    date: Datetime.moment("01 May 2020")
  }]
}

And your validation function should look something like this or you can extract it into a class method

  let valid = function(current, selected) {
    return !this.state.dates.some(day => current.isSame(day.date, "day"));
  };

and pass this function to Datetime

<Datetime timeFormat={false} isValidDate={valid}/>
Sign up to request clarification or add additional context in comments.

1 Comment

How can we disable both - past expired days along with - future booked dates

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.