i want set format of md-datepicker to dd-mm-yyyy

user3248761 picture user3248761 · Feb 7, 2017 · Viewed 12.4k times · Source

datepicker of angular material. i want to keep change format of date picker to 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');
};
});

but i want one time configuration for all date picker in application. i think this example is for single date picker.

Answer

Shilpakar Amar picture Shilpakar Amar · Jul 13, 2017

You can do it easily from below code,

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);
    };
});