我需要根据某些用户设置以两种不同的格式显示日期。
比方说 "YYYY-MM-DD"
和 "DD/MM/YYYY"
。
我知道我必须编写自定义 DateAdapter
(我已经这样做了),但是是否可以使用例如在两种格式之间切换下拉列表?
附言
我也在使用 Moment.js。
最佳答案
您的自定义日期格式位于 TS 文件中。
这意味着您可以访问 Javascript 和 Typescript。
你可以做的是使用 localStorage 来存储格式,并以你的日期格式:
export const CUSTOM_DATE_FORMAT = {
parse: {
dateInput: localStorage.getItem('dateFormat'),
},
display: {
dateInput: localStorage.getItem('dateFormat'),
monthYearLabel: 'MMMM YYYY',
dateA11yLabel: 'DD/MM/YYYY',
monthYearA11yLabel: 'MMMM YYYY',
},
};
我想你会弄清楚如何在本地存储中存储格式 ;)
关于Angular Material2 日期选择器 - 动态格式化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48317038/