如何解决 RangeError:无效的时间值

IT技术 reactjs utc daterangepicker
2021-05-06 02:47:53

利用 React,我使用 moment 和 react-datepicker 库设置了一个 DatePicker 组件,并将使用今天的日期以 MM-DD-YYYY 格式计算的初始日期值传递给它。

当我尝试加载时,出现此错误行:

throw new RangeError('时间值无效'); 将系统时区中的日期转换为 UTC+00:00 时区中的相同日期。这样可以确保在实现 UTC 功能时,语言环境将与其兼容。`

日期选择器组件:

class DatePicker extends Component {
  render() {
    return (
      // <input type="date" 
      //   className={this.props.className + " myInput"} 
      //   placeholder={this.props.placeholder} 
      //   onChange={this.props.onChange} 
      //   value={this.props.value}
      // />
        <DatePicker2
          dateFormat="DD MMM YYYY"
          className={this.props.className + " myInput"} 
          selected={moment(this.props.value)}
          onChange={this.props.onChange} 
          readOnly={true}
        />
    );
  }
}

export default DatePicker;

我的问题是:如何将系统时区中的日期转换为 UTC+00:00 时区中的相同日期(假设这是解决方案,但如果不是,我该如何解决?

1个回答

要重新格式化您可以使用的日期:

moment(this.props.value).format("YYYY-MM-DD HH:mm Z");