md-datepicker输入格式

Dor*_*n M 11 angularjs angular-material

在角度材质中使用md-datepicker指令时,日期格式似乎不适用于直接输入.

如果我在日历中选择日期,它将按指定显示(在我的情况下为DD-MM-YYYY),但如果我尝试手动更改输入,则我的条目将被分析为MM-DD-YYYY.

到目前为止,我的datepicker是使用此问题的代码设置的

angular.module('MyApp').config(function($mdDateLocaleProvider) {
  $mdDateLocaleProvider.formatDate = function(date) {
    return date ? moment(date).format('DD-MM-YYYY') : '';
  };
});
Run Code Online (Sandbox Code Playgroud)

是一个用于查看实际问题的代码.

有没有办法设置输入格式?

Kur*_*ven 19

格式化日期事件是不够的.您还应该配置解析日期事件.

$mdDateLocaleProvider.parseDate = function(dateString) {
  var m = moment(dateString, 'DD-MM-YYYY', true);
  return m.isValid() ? m.toDate() : new Date(NaN);
};
Run Code Online (Sandbox Code Playgroud)

请参阅更新后的笔:http://codepen.io/anon/pen/GpBpwZ?edit = 101


Het*_*dev 5

完整的解决方案基于:

$mdDateLocaleProvider.formatDate = function(date) {
 return date ? moment(date).format('DD-MM-YYYY') : '';
};

$mdDateLocaleProvider.parseDate = function(dateString) {
 var m = moment(dateString, 'DD-MM-YYYY', true);
 return m.isValid() ? m.toDate() : new Date(NaN);
};
Run Code Online (Sandbox Code Playgroud)