javascript 在材质 ui 的日期选择器中更改 formatDate
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46543391/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change formatDate in datepicker of material ui
提问by user7334203
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:
我使用带有 redux 形式的 material-ui datepicker 组件。我这里有一个小问题,这看起来很神奇。当我更改日期时,它在我的输入字段中显示为 yyyy-mm-dd。我想改变它以显示为 dd-mm-yyyy。datepicker 有一个名为 formatDate 的属性,它接受一个函数作为输入。所以我写道:
<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?
但它似乎不起作用。你有什么想法?
回答by Mayank Shukla
As per DOC:
根据DOC:
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.
接收所选日期作为 formatDate 函数的参数,更改日期格式并将格式化程序值作为字符串返回。
Another change is:
另一个变化是:
format(DD-MM-YYYY)
DD-MM-YYYYshould be a string like this:
DD-MM-YYYY应该是这样的字符串:
format('DD-MM-YYYY')
Write it like this:
像这样写:
<Field
name="dateFrom"
component={DatePicker}
hintText="Ημερομην?α απ?"
autoOk
formatDate={(date) => moment(date).format('DD-MM-YYYY')}
/>

