Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/367.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 尝试比较日期值以返回有效间隔_Javascript_Reactjs_Date - Fatal编程技术网

Javascript 尝试比较日期值以返回有效间隔

Javascript 尝试比较日期值以返回有效间隔,javascript,reactjs,date,Javascript,Reactjs,Date,我正在为一个预订网站编写代码,并试图自己编写间隔的逻辑代码。代码应该比较“自”和“直到”日期,如果选择的“自”在“之前”之后,则“之前”应自动设置为“自”加上一天。虽然当两个日期在同一个月(比如“before==2020-12-12”,“after==2020-20-11”)时它似乎是有效的,但如果我尝试比较类似的东西(比如“before==2021-01-12”,“after==2020-20-11”),它不会起作用 代码在这里,非常感谢 the input looks like this

我正在为一个预订网站编写代码,并试图自己编写间隔的逻辑代码。代码应该比较“自”和“直到”日期,如果选择的“自”在“之前”之后,则“之前”应自动设置为“自”加上一天。虽然当两个日期在同一个月(比如“before==2020-12-12”,“after==2020-20-11”)时它似乎是有效的,但如果我尝试比较类似的东西(比如“before==2021-01-12”,“after==2020-20-11”),它不会起作用

代码在这里,非常感谢

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)
      });
    }
  };```
在比较中尝试使用
getTime()
而不是
valueOf()


公认的答案很好地解释了日期比较。

不幸的是,它不起作用。问题似乎并不在于比较本身,因为日期会改变,但它只会改变一个月的哪一天,而不是月份或年份。