Change format of md-datepicker in Angular Material with momentjs

前端 未结 13 831
再見小時候
再見小時候 2020-11-28 09:01

Angular material introduced a new date picker component found here.

I want the date returned by this component to be in the format yyy-mm-dd but I am not su

13条回答
  •  没有蜡笔的小新
    2020-11-28 09:44

    Changing date format, month names and week names during runtime is perfectly possible with AngularJS 1.5.9 and moment 2.17.1.

    First configure the initial language. (In this example the configuration of angular-translate/$translateProvider is optional.)

    angular.module('app').config(configureLocalization)
    
    configureLocalization.$inject = ['$translateProvider', '$mdDateLocaleProvider', 'localdb', '__config'];
    function configureLocalization($translateProvider, $mdDateLocaleProvider, localdb, __config) {
      // Configure angular-translate
      $translateProvider.useStaticFilesLoader({
          prefix: 'locale/',
          suffix: '.json'
      });
      // get the language from local storage using a helper 
      var language = localdb.get('language');
      if (!language || language === 'undefined') {
        localdb.set('language', (language = __config.app.defaultLanguage));
      }
      // Set the preferredLanguage in angular-translate
      $translateProvider.preferredLanguage(language);
    
      // Change moment's locale so the 'L'-format is adjusted.
      // For example the 'L'-format is DD.MM.YYYY for German
      moment.locale(language);
    
      // Set month and week names for the general $mdDateLocale service
      var localeData = moment.localeData();
      $mdDateLocaleProvider.months      = localeData._months;
      $mdDateLocaleProvider.shortMonths = moment.monthsShort();
      $mdDateLocaleProvider.days        = localeData._weekdays;
      $mdDateLocaleProvider.shortDays   = localeData._weekdaysMin;
      // Optionaly let the week start on the day as defined by moment's locale data
      $mdDateLocaleProvider.firstDayOfWeek = localeData._week.dow;
    
      // Format and parse dates based on moment's 'L'-format
      // 'L'-format may later be changed
      $mdDateLocaleProvider.parseDate = function(dateString) {
        var m = moment(dateString, 'L', true);
        return m.isValid() ? m.toDate() : new Date(NaN);
      };
    
      $mdDateLocaleProvider.formatDate = function(date) {
        var m = moment(date);
        return m.isValid() ? m.format('L') : '';
      };
    }
    

    Later you may have some base controller that watches a language variable which is changed when the user selects another language.

    angular.module('app').controller('BaseCtrl', Base);
    
    Base.$inject = ['$scope', '$translate', 'localdb', '$mdDateLocale'];
    function Base($scope, $translate, localdb, $mdDateLocale) {
    
      var vm = this;
      vm.language = $translate.use();
    
      $scope.$watch('BaseCtrl.language', function(newValue, oldValue) {
        // Set the new language in local storage
        localdb.set('language', newValue);
        $translate.use(newValue);
    
        // Change moment's locale so the 'L'-format is adjusted.
        // For example the 'L'-format is DD-MM-YYYY for Dutch
        moment.locale(newValue);
    
        // Set month and week names for the general $mdDateLocale service
        var localeDate = moment.localeData();
        $mdDateLocale.months      = localeDate._months;
        $mdDateLocale.shortMonths = moment.monthsShort();
        $mdDateLocale.days        = localeDate._weekdays;
        $mdDateLocale.shortDays   = localeDate._weekdaysMin;
        // Optionaly let the week start on the day as defined by moment's locale data
        $mdDateLocale.firstDayOfWeek = localeData._week.dow;
      });
    }
    

    Notice how we don't need to change the $mdDateLocale.formatDate and $mdDateLocale.parseDate methods as they are already configured to use the 'L'-format that is changed by calling moment.locale(newValue).

    See the documentation for $mdDateLocaleProvider for more locale customization: https://material.angularjs.org/latest/api/service/$mdDateLocaleProvider

    Bonus: This is how the language selector may look like:

    
      
      
    
    

提交回复
热议问题