Angular bootstrap datepicker date format does not format ng-model value

后端 未结 13 1375
借酒劲吻你
借酒劲吻你 2020-12-01 04:24

I am using bootstrap date-picker in my angular application. However when I select a date from that date-picker underlying ng-model that I have bind gets updated I want that

13条回答
  •  不知归路
    2020-12-01 04:38

    All proposed solutions didn't work for me but the closest one was from @Rishii.

    I'm using AngularJS 1.4.4 and UI Bootstrap 0.13.3.

    .directive('jsr310Compatible', ['dateFilter', 'dateParser', function(dateFilter, dateParser) {
      return {
        restrict: 'EAC',
        require: 'ngModel',
        priority: 1,
        link: function(scope, element, attrs, ngModel) {
          var dateFormat = 'yyyy-MM-dd';
    
          ngModel.$parsers.push(function(viewValue) {
            return dateFilter(viewValue, dateFormat);
          });
    
          ngModel.$validators.date = function (modelValue, viewValue) {
            var value = modelValue || viewValue;
    
            if (!attrs.ngRequired && !value) {
              return true;
            }
    
            if (angular.isNumber(value)) {
              value = new Date(value);
            }
    
            if (!value) {
              return true;
            }
            else if (angular.isDate(value) && !isNaN(value)) {
              return true;
            }
            else if (angular.isString(value)) {
              var date = dateParser.parse(value, dateFormat);
              return !isNaN(date);
            }
            else {
              return false;
            }
          };
        }
      };
    }])
    

提交回复
热议问题