React datepicker - 无效的时间值

React datepicker - invalid time value

我正在尝试创建一个 React-datepicker;但是,我收到此错误:

RangeError: Invalid Time Value

我添加了一个 dateFormat 来匹配我的日期变量;但是,我仍然遇到同样的错误?

我做错了什么?

import DatePicker from "react-datepicker";

let date = moment(dateString).format('MMMM d, YYYY h:mm a'); // January 3, 2019 12:30 pm

<DatePicker
    selected={date}
    onChange={this.handleInputChange}
    showTimeSelect
    timeFormat="HH:mm"
    timeIntervals={15}
    dateFormat="MMMM d, YYYY h:mm a"
    timeCaption="time"
/>

我也试过 dateFormat="MMMM d, yyyy h:mm aa"

A DatePicker 需要 Date,而不是 moment。你必须先转换它:

let date = moment(dateString).toDate();

来自日期选择器docs:

Up until version 1.8.0, this package was using Moment.js. Starting v2.0.0, we switched to using native Date objects to reduce the size of the package. If you're switching from 1.8.0 to 2.0.0 or higher, please see the updated example above of check out the examples site for up to date examples.

在我的例子中,问题涉及在从数据库中提取 ISO 格式的日期后检查日期是否为空。

 <DatePicker selected={initiated_Date ? new Date(initiated_Date) : null}