我想将md-datepicker的设置格式设置为dd-mm-yyyy

use*_*761 6 material angularjs

角度材料的datepicker.我想保持日期选择器的更改格式为dd-mm-yyyy.

angular.module('MyApp')
.controller('AppCtrl', function($scope) {
  $scope.myDate = new Date();

  $scope.minDate = new Date(
    $scope.myDate.getFullYear(),
    $scope.myDate.getMonth() - 2,
    $scope.myDate.getDate());

  $scope.maxDate = new Date(
    $scope.myDate.getFullYear(),
    $scope.myDate.getMonth() + 2,
    $scope.myDate.getDate());    }).config(function($mdDateLocaleProvider) {
    $mdDateLocaleProvider.formatDate = function(date) {
return moment(date).format('YYYY-MM-DD');
};
});
Run Code Online (Sandbox Code Playgroud)

但我希望在应用程序中为所有日期选择器进行一次配置.我认为这个例子是针对单日期选择器的.

小智 5

您可以通过下面的代码轻松完成,

app.config(function($mdDateLocaleProvider) {
    $mdDateLocaleProvider.formatDate = function(date) {
        if (!date) {return '';}
        else{
            return 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)


use*_*761 1

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