我正在尝试创建一个 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"。
最佳答案
DatePicker
需要 Date
,而不是 moment
。你必须先转换它:
let date = moment(dateString).toDate();
来自 DatePicker 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.
关于reactjs - react 日期选择器 - 无效的时间值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57098402/