如何更改资料2日期选择器的输出格式 [英] How to change output format of material 2 date-picker
问题描述
我正在使用角形材料2 日期选择器组件
I am using angular material 2 Date-Picker component
它唯一理解的字符串是ISO日期格式
The only string input it understands is in ISO date format
例如"2017-11-13T10:39:28.300Z"
Ex. "2017-11-13T10:39:28.300Z"
但是我想用区域设置日期值修补我的表单控件
But i want to patch my form control with locale date value
例如"2017年11月13日,下午4:09:46"
Ex. "11/13/2017, 4:09:46 PM"
这样它就可以输出更高的格式,甚至可以期待这种格式.
So that it output the later format and even expects this format.
我该怎么做?有没有办法不使用ISO而是使用自定义格式?
How can i do this? Is there a way to not use ISO but custom formats?
一些想法:
我应该写customDateAdaptor吗?
Should i write customDateAdaptor?
更新:
https://stackblitz.com/edit/angular-material -moment-adapter-example-bqvm2f
我尝试通过扩展nativeDateAdaptor来实现自定义dateAdaptor
i have tried to implement custom dateAdaptor by extending nativeDateAdaptor
推荐答案
最好的解决方案是使用ControlValueAccessor并制作一个自定义输入组件,这将使您可以控制该组件的输入和输出.
The best solution would be to use ControlValueAccessor and make a custom input component, this will have you in control of input and output of the component.
更新:
以下是使用Moutaz-homsi复制的这种方法的参考实现
Here is the reference implementation using this approach reproduced by Moutaz-homsi
https://stackblitz.com/edit/angular-dlxnmx?file = app%2Fcva-date.component.ts
这篇关于如何更改资料2日期选择器的输出格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!