reactjs - react 日期选择器 - 无效的时间值

标签 reactjs datepicker react-datepicker

我正在尝试创建一个 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/

相关文章:

c# - 更改日期选择器文本 : capitalize month

javascript - 安装 $npm 后,出现错误 : Cannot find module '../lib/utils/unsupported.js'

javascript - react ( typescript ): How to loop inside const creation

php - 带 Bootstrap 日期选择器的 Laravel 4

jquery - bootstrap-datepicker-rails - 选择日期将不会保留

javascript - 修改react-big-calendar中的工具提示

javascript - React DatePicker 切换问题

css - react-datepicker 输入宽度不会调整为 100%

javascript - React - 重定向后调用方法

javascript - react.js 中的键绑定(bind)