1

利用 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 时区中的相同日期(假设这是解决方案,但如果不是,我该如何解决?

4

1 回答 1

1

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

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

于 2019-08-01T18:59:25.310 回答