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

4

7

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.

Arrogant answered 7/2, 2017 at 15:38 Comment(4)
#32566916Shop
is it the uib-datepicker? Then this should help #23343871Yammer
Possible duplicate of Angular bootstrap datepicker date format does not format ng-model valueIrtysh
done by angular.module('app').config(function($mdDateLocaleProvider) { $mdDateLocaleProvider.formatDate = function(date) { return moment(date).format('YYYY-MM-DD'); }; });Arrogant
T
5

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);
    };
});
Thiosinamine answered 13/7, 2017 at 1:42 Comment(0)
A
1
angular.module('app').config(function($mdDateLocaleProvider) {
    $mdDateLocaleProvider.formatDate = function(date) {
       return moment(date).format('YYYY-MM-DD');
    };
});
Arrogant answered 7/2, 2017 at 16:44 Comment(1)
@MuhamedShafeeq momentjs must be included in your project to use it.Alded
W
0

If you are using the latest version of angular-material.js then use the $mdDateLocale service. This code sample uses angular's built in date filter as an alternative to using the moment.js library. See other date format options using angular's $filter service here at this link https://docs.angularjs.org/api/ng/filter/date.

// mainController.js
angular.module('app').config(function($mdDateLocale, $filter, $scope) {

      // FORMAT THE DATE FOR THE DATEPICKER
      $mdDateLocale.formatDate = function(date) {
            return $filter('date')($scope.myDate, "dd-MM-yyyy");
      };

});
Windburn answered 12/4, 2017 at 20:2 Comment(0)
W
0

Use the Javascript Library to achieve this

Add this Moment-with locals.min.js file to your page

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.6/moment-with-locales.min.js"></script>

And then in your application Config Module..

angular.module('timeApp').config(function ($mdDateLocaleProvider) {

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

})

In Html Page

 <md-datepicker ng-model="dateofbirth" md-current-view="year" md-placeholder="Enter Date of Birth" md-open-on-focus></md-datepicker>
Wivinia answered 29/8, 2017 at 10:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.