在材质 ui 的日期选择器中更改 formatDate [英] Change formatDate in datepicker of material ui

查看:22
本文介绍了在材质 ui 的日期选择器中更改 formatDate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用带有 redux 形式的 material-ui datepicker 组件.我这里有一个小问题,这看起来很神奇.当我更改日期时,它在我的输入字段中显示为 yyyy-mm-dd.我想改变它以显示为 dd-mm-yyyy.datepicker 有一个名为 formatDate 的属性,它接受一个函数作为输入.所以我写道:

i using material-ui datepicker component with redux form. It looks amazing by i have a little issue here. When i change the date it appears in my input field as yyyy-mm-dd. I want to change it so as to appear as dd-mm-yyyy. The datepicker has a property called formatDate which takes a function as an input. So i wrote:

<Field
      name="dateFrom"
      component={DatePicker}
      hintText="Ημερομηνία από"
      autoOk
      formatDate={() => moment().format(DD-MM-YYYY)}
    />

但它似乎不起作用.你有什么想法吗?

but it does not seem to work. Do you have any ideas?

推荐答案

根据 DOC:

As per DOC:

formatDate ====> 函数 ====> 这个函数被调用来格式化输入字段中显示的日期,应该返回一个字符串.

formatDate ====> function ====> This function is called to format the date displayed in the input field, and should return a string.

签名:函数(日期:对象)=> 任何日期:要成为的日期对象格式化.返回(任何):格式化的日期.

Signature: function(date: object) => any date: Date object to be formatted. returns (any): The formatted date.

<小时>

接收选择的日期作为 formatDate 函数的参数,改变日期的格式并以字符串形式返回格式化程序值.


Receive the selected date as an argument of formatDate function, change the format of the date and return the formatter value as a string.

另一个变化是:

format(DD-MM-YYYY)

DD-MM-YYYY 应该是这样的字符串:

DD-MM-YYYY should be a string like this:

format('DD-MM-YYYY')

这样写:

<Field
      name="dateFrom"
      component={DatePicker}
      hintText="Ημερομηνία από"
      autoOk
      formatDate={(date) => moment(date).format('DD-MM-YYYY')}
/>

这篇关于在材质 ui 的日期选择器中更改 formatDate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆