如何更改材料 2 日期选择器的输出格式 [英] How to change output format of material 2 date-picker
问题描述
我正在使用 angular material 2 Date-Picker 组件
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
例如.11/13/2017,下午 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-瞬间适配器示例-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屋!