2

In google chrome everything works ok. When I open the application in mozilla React Date Picker returns invalid date

I tried to use the moment(this.state.startDate).toDate()

new Date(this.state.startDate) but it doesn't work

class Example extends React.Component {
  state = {
    startDate: new Date()
  };

  handleChange = date => {
    this.setState({
      startDate: date
    });
  };

  render() {

    return (
      <DatePicker
        selected={this.state.startDate}
        onChange={this.handleChange}
      />
    );
  }
}
Umbro
  • 1,984
  • 12
  • 40
  • 99

2 Answers2

1

Try adding dateFormat property like:

      <DatePicker
        selected={this.state.startDate}
        onChange={this.handleChange}
        dateFormat="d MMM yyyy"
      />
Tarreq
  • 1,282
  • 9
  • 17
-1

You can try this approach this code is working perfectly in my project in Mozilla, chrome, and edge. My date picker version is "react-datepicker": "^2.7.0", and react version is "react": "^16.8.6",

filters:{
                    startDate: new Date(),
                    endDate: new Date(),
                    type: props.location.state === undefined ? 0 : props.location.state.reportType,
                    reportName : 'Not Selected',
                    reportPreviewCount: 0,
                    reportedUserId: -1,
                    userType: '0',
                },    


    handleChangeDate(date, dateType) {
            let oldState = this.state.filters;

            if(dateType === 'startDate'){
                oldState['startDate'] = date;
                oldState['reportPreviewCount'] = 0;

            }else{
                oldState['endDate'] = date;
                oldState['reportPreviewCount'] = 0;
            }
            this.setState({titleDatatable: [], dataSetDataable: [], filters: oldState, flashState: true});
        }     


                          <DatePicker
                                selected= {props.filters.startDate}
                                onChange={(date) => props.handleChangeDate(date, 'startDate')}
                                className="form-control"
                                peekNextMonth
                                showMonthDropdown
                                showYearDropdown
                                dropdownMode="select"
                                onChangeRaw={(e) => e.preventDefault()}
                                withPortal
                            />
ANIK ISLAM SHOJIB
  • 3,002
  • 1
  • 27
  • 36