0

I am coding a reservation website and trying to code the logic for the intervals myself. The code is supposed to compare the "since" and "until" dates, and if the "since" selected comes after "before", before should automatically set to "since" plus one day. Although it seems to work when both dates are on the same month (like "before==2020-12-12", "after==2020-20-11") if I try to compare something like this (like "before==2021-01-12", "after==2020-20-11") it won't work.

Here goes the code, and thanks a lot.

the input looks like this
   //2021-01-13T00:00:00-03:00

handleDateChange = (limit, value) => {
    if (limit === "since") {
      if (this.state.until === "") {
        let until = new Date(value);
        this.setState({
          [limit]: new Date(value),
          until: new Date(until.setDate(new Date(value).getDate() + 1))
        });
      } else if (
        new Date(value).valueOf() > new Date(this.state.until).valueOf()
      ) {
        console.log(value.valueOf());
        console.log(this.state.until.valueOf());
        console.log(new Date(this.state.until).valueOf());
        this.setState({
          [limit]: new Date(value),
          until: new Date(
            this.state.until.setDate(new Date(value).getDate() + 1)
          )
        });
      }
    } else if (limit === "until" && this.state.since === "") {
      this.setState({
        until: new Date(value),
        since: new Date()
      });
    } else {
      this.setState({
        ...this.state,
        [limit]: new Date(value)
      });
    }
  };```
M0llzilla
  • 33
  • 5

1 Answers1

0

Try using getTime() instead of valueOf() in your comparisons.

The accepted answer here provides a good explanation of date comparisons.

groul
  • 119
  • 1
  • 9
  • Unfortunately it didn't work. The problem does not seem to reside in the comparison itself, since the date changes, but it only changes the day of the month, not the month or the year. – M0llzilla Nov 11 '20 at 18:24