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

后端 未结 13 1271
借酒劲吻你
借酒劲吻你 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:48

    Finally I got work around to the above problem. angular-strap has exactly the same feature that I am expecting. Just by applying date-format="MM/dd/yyyy" date-type="string" I got my expected behavior of updating ng-model in given format.

    <div class="bs-example" style="padding-bottom: 24px;" append-source>
        <form name="datepickerForm" class="form-inline" role="form">
          <!-- Basic example -->
          <div class="form-group" ng-class="{'has-error': datepickerForm.date.$invalid}">
            <label class="control-label"><i class="fa fa-calendar"></i> Date <small>(as date)</small></label>
            <input type="text"  autoclose="true"  class="form-control" ng-model="selectedDate" name="date" date-format="MM/dd/yyyy" date-type="string" bs-datepicker>
          </div>
          <hr>
          {{selectedDate}}
         </form>
    </div>
    

    here is working plunk link

    0 讨论(0)
  • 2020-12-01 04:50

    Although similar answers have been posted I'd like to contribute what seemed to be the easiest and cleanest fix to me. Assuming you are using the AngularUI datepicker and your initial value for the ng-Model does not get formatted simply adding the following directive to your project will fix the issue:

    angular.module('yourAppName')
    .directive('datepickerPopup', function (){
        return {
            restrict: 'EAC',
            require: 'ngModel',
            link: function(scope, element, attr, controller) {
          //remove the default formatter from the input directive to prevent conflict
          controller.$formatters.shift();
      }
    }
    });
    

    I found this solution in the Github AngularUI issues and therefore all credit goes to the people over there.

    0 讨论(0)
  • 2020-12-01 04:54

    Steps to change the default date format of ng-model

    For different date formats check the jqueryui datepicker date format values here for example I have used dd/mm/yy

    Create angularjs directive

    angular.module('app', ['ui.bootstrap']).directive('dt', function () {
    return {
        restrict: 'EAC',
        require: 'ngModel',
        link: function (scope, element, attr, ngModel) {
            ngModel.$parsers.push(function (viewValue) {
               return dateFilter(viewValue, 'dd/mm/yy');
            });
        }
     }
    });
    

    Write dateFilter function

    function dateFilter(val,format) {
        return $.datepicker.formatDate(format,val);
    }
    

    In html page write the ng-modal attribute

    <input type="text" class="form-control" date-type="string"  uib-datepicker-popup="{{format}}" ng-model="src.pTO_DATE" is-open="popup2.opened" datepicker-options="dateOptions" ng-required="true" close-text="Close" show-button-bar="false" show-weeks="false" dt />
    
    0 讨论(0)
  • 2020-12-01 04:58

    You can make use of $parsers as shown below,this solved it for me.

    window.module.directive('myDate', function(dateFilter) {
      return {
        restrict: 'EAC',
        require: '?ngModel',
        link: function(scope, element, attrs, ngModel) {
          ngModel.$parsers.push(function(viewValue) {
            return dateFilter(viewValue,'yyyy-MM-dd');
          });
        }
      };
    });
    

    HTML:

    <p class="input-group datepicker" >
      <input
         type="text"
         class="form-control"
         name="name"
         datepicker-popup="yyyy-MM-dd"
         date-type="string"
         show-weeks="false"
         ng-model="data[$parent.editable.name]" 
         is-open="$parent.opened"
         min-date="minDate"
         close-text="Close"
         ng-required="{{editable.mandatory}}"
         show-button-bar="false"
         close-on-date-selection="false"
         my-date />
      <span class="input-group-btn">
        <button type="button" class="btn btn-default" ng-click="openDatePicker($event)">
          <i class="glyphicon glyphicon-calendar"></i>
        </button>
      </span>
    </p>
    
    0 讨论(0)
  • 2020-12-01 04:58

    I ran into the same problem and after a couple of hours of logging and investigating, I fixed it.

    It turned out that for the first time the value is set in a date picker, $viewValue is a string so the dateFilter displays it as is. All I did is parse it into a Date object.

    Search for that block in ui-bootstrap-tpls file

      ngModel.$render = function() {
        var date = ngModel.$viewValue ? dateFilter(ngModel.$viewValue, dateFormat) : '';
        element.val(date);
    
        updateCalendar();
      };
    

    and replace it by:

      ngModel.$render = function() {
        ngModel.$viewValue = new Date(ngModel.$viewValue);
        var date = ngModel.$viewValue ? dateFilter(ngModel.$viewValue, dateFormat) : '';
        element.val(date);
    
        updateCalendar();
      };
    

    Hopefully this will help :)

    0 讨论(0)
  • 2020-12-01 05:02

    With so many answers already written, Here's my take.

    With Angular 1.5.6 & ui-bootstrap 1.3.3 , just add this on the model & you are done.

    ng-model-options="{timezone: 'UTC'}" 
    

    Note: Use this only if you are concerned about the date being 1 day behind & not bothered with extra time of T00:00:00.000Z

    Updated Plunkr Here :

    http://plnkr.co/edit/nncmB5EHEUkZJXRwz5QI?p=preview

    0 讨论(0)
提交回复
热议问题