How to format an inital date value using the AnglerJS ui-date directive?

When I get the model from the server, it looks like this:

$scope.m = 
{
   name: "John",
   Dt:   "2013-10-03T18:47:33.5049087-07:00"
};

The view looks like this:

<input title="Date" ui-date ng-model="m.Dt" />

I set the default date format for the datejp jQuery parameter:

$.datepicker.setDefaults({dateFormat: 'mm-dd-yy'});

The initial input value remains "2013-10-03T18: 47: 33.5049087-07: 00". It only formats as mm-dd-yyif I use datepicker to change the date.

How can I get the initial value in format mm-dd-yy?

+4
source share
3 answers

The $ scope.m.Dt property must be of type date , not string.

$scope.m = 
{
   name: "John",
   Dt:   new Date()
};

, ui-date-format, :

<input title="Date" ui-date ui-date-format="mm-dd-yy" ng-model="m.Dt" />

. readme: https://github.com/angular-ui/ui-date#ui-date-format-directive

+1

, "" - , Angular Date ( ).

: .

, , . , date, JSON $http success :

$http.get('data.json').success(function(data) {
    data.date = Date.parse(data.date);
    $scope.model = data;
}

$scope, Angular $scope.model.date JS Date.

$watch . - :

$scope.$watch('model.date', function() {
    if (typeof $scope.model.date === 'string') {
        $scope.model.date = Date.parse($scope.model.date);
    }
});

, $scope.model.date. , , .

0

. , JQuery-UI Angularjs

"2015-03-24T04: 00: 00"

, , .

var date = "2015-03-24T04:00:00"
var formattedDate = date.match(/[\d-]+/).pop();
// formattedDate is now "2015-03-24" which is passed into
// the directive below as the input.$modelValue.

...

// Here is directive example.
link: function( scope, element, attrs, input ){

  element.datepicker( optionsObjectHere );
  setInitialDateFormatOnInput();

  function setInitialDateFormatOnInput(){
    setTimeout(function(){ // This timeout is required to delay the directive for the input.modelValue to resolve, however, no actual delay occurs!
      element.datepicker( "setDate", formatToJqueryUIDateFormat());
    });
  }

  function formatToJqueryUIDateFormat(){
    return $.datepicker.parseDate( 'yy-mm-dd', input.$modelValue );
    // 'yy-mm-dd' needs to match the input.$modelValue format from above. 
  }
} // link

jquery .

HTML

<input type="text" class="inline" ng-model="inputValue" my-calendar-popup="calendarOptions" />

calendarOptions

  var calendarOptions = { minDate: 0, buttonImage: "calendar-icon.png", buttonImageOnly: 'true', showOn: "both", dateFormat: "MM d, yy" };

app.directive('myCalendarPopup', function(){

  var defaultOptions = { minDate: 0, buttonImage: "calendar-icon.png",   buttonImageOnly: 'true', showOn: "both", dateFormat: "MM d, yy" };
  // defaultOptions just in case someone doesn't pass in options.

  return {
    require:'?ngModel',
    restrict: 'A',

    link: function( scope, element, attrs, input ){
      if ( !input ){ return; } // If no ngModel then return;

      element.datepicker( createCalendarOptions());
      setInitialDateFormatOnInput();

      function createCalendarOptions(){
        if( !attrs.rsCalendarPopup ){ return addRequiredJqueryFunction( defaultOptions );}
        return formatOptions();
      }

      function formatOptions() {
        var options = scope.$eval( attrs.rsCalendarPopup );
        // Turn string into object above.
        return addRequiredJqueryFunction( options );
      }

      function addRequiredJqueryFunction( options ){
        options.onSelect = changeDate;
        // add onSelect to passed in object and reference local changeDate function, which will update changes to input.$modelValue.
        return options;
      }

      function changeDate( date ){
        input.$setViewValue( date );
      }

      function setInitialDateFormatOnInput(){
        setTimeout(function(){
        // This timeout is required to delay the directive for the input.modelValue to resolve.
        // However, there is no actual timeout time. This is a must to get
        // Angular to behave.
          element.datepicker( "setDate", formatToJqueryUIDateFormat());
        });
      }

      function formatToJqueryUIDateFormat(){
        return $.datepicker.parseDate( 'yy-mm-dd', input.$modelValue );
        // 'yy-mm-dd' is not the format you want the calendar to be
        // it is the format the original date shows up as.
        // you set your actual formatting using the calendar options at
        // the top of this directive or inside the passed in options.
        // The key is called dateFormat, in this case it set as
        // dateFormat: "MM d, yy" which makes June 30, 2015.
      }
    } // link
  } // return
});

Note. I can get this to work only in the displayed configuration. I added it to the controller and even to the controller directive and was unable to recreate the initial date condition. I did not find out why this is so far. Perhaps this solution only works in the link function, which is built into another isolated area directive and works because of a certain time.

0
source

All Articles