user10298495
user10298495

Reputation:

Compare two dates in react

I am using react-datepicker. Here is the docs link.

I have two input box for dates.One is for "Starting date" and another is for "ending date". what i need is end date is not less than the starting date.

I am running the below code which is not working fine.

 handleEndingDateChange(date) {
if(this.state.startingDate){
  if(this.state.startingDate<date){
    console.log('deadline date should be greater than starting date.')
    this.setState({
      deadLineError:(!this.state.deadLineError),
      endingDate: date
    })
  }else{
    console.log('date ok!');
    this.setState({
      deadLineError:(!this.state.deadLineError),
      endingDate: date,
    });
  }      
}
}

Can anyone please help me to solve this issue.

Upvotes: 0

Views: 8384

Answers (1)

Yossi
Yossi

Reputation: 6027

Try:

if (startDate.getTime() <= endDate.getTime()) {
  ...
} else {
  ...
}

Upvotes: 1

Related Questions