【问题标题】:How to change the format of a reactjs material ui date picker如何更改 reactjs 材料 ui 日期选择器的格式
【发布时间】:2019-02-24 05:38:21
【问题描述】:

我只需要一个超级简单的选择器,有人可以在其中输入日期和/或从日历中选择它。 material-ui 选择器满足了我的需求。

但是,它以 MM/DD/YYYY 显示日期(即使它在内部存储 YYYY-MM-DD,如果您吐出 TextField 的值)

您可以在此处的演示中看到这一点:https://material-ui.com/demos/pickers/

我希望显示的日期为 YYYY/MM/DD(或 YYYY-MM-DD)。

我已经尝试构建一个 formatDate 函数,正如关于此文档和一些旧文档的许多其他问题中所述...但这导致我收到 React does not recognize theformatDateprop on a DOM element. If you intentionally want it to appear in the DOM as a custom attribute, spell it as lowercaseformatdateinstead. 错误。

这也出现在 git 讨论中,并且没有任何真正的解决方案就关闭了该主题:https://github.com/mui-org/material-ui/issues/10251

肯定有办法做到这一点...帮助?

【问题讨论】:

    标签: reactjs material-ui


    【解决方案1】:

    这个组件是用<input>标签构建的,根据this source,“显示的日期格式将根据用户浏览器设置的区域来选择”。

    【讨论】:

    • 你知道我会如何改变吗?
    • 我认为您需要更改浏览器的语言,但它对我不起作用。
    • 这是您的相同问题,但专门针对 标签:stackoverflow.com/questions/7372038/…
    【解决方案2】:

    你能提供一个你目前拥有的代码sn-p吗?

    formatDate 回调是接受的转换值的方法,我不确定你为什么会收到错误,正确的代码 sn-p 是这样的:

    formatDate={(date) => moment(date).format('DD-MM-YYYY')}

    【讨论】:

    • 我相信这是一个旧功能,因为它不再有效(如我的 OP 中所述)
    【解决方案3】:

    直接来自 DatePicker 的文档。试试这个

    <DatePicker 
            inputFormat="MM/dd/yyyy"
            hintText="Date of purchase"
            value={this.state.controlledDate}
            onChange={this.props.onChange}
          />
    

    inputFormat 通常应该可以解决问题。你可以随意改变它,它也可以用于 dateTimePicker。

    例如,

    inputFormat="yyyy/MM/dd"
    

    或任何其他方式

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-14
      • 2021-02-22
      • 1970-01-01
      • 2019-03-04
      • 2019-06-02
      相关资源
      最近更新 更多