Error: [ngModel:datefmt] Expected `2015-05-29T19:06:16.693209Z` to be a date - Angular

Solution 1:

This must be happening with angular 1.3+. 1.3+ on wards ng-model for date/time input needs to be a valid date object, string representation of date is no longer allowed. You need to convert string to date object ($scope.created_time = new Date(dateString)) and bind it to the ng-model. If you follow the error link it has a clear description about the error and how to resolve it.

All date-related inputs like require the model to be a Date object. If the model is something else, this error will be thrown. Angular does not set validation errors on the in this case as those errors are shown to the user, but the erroneous state was caused by incorrect application logic and not by the user.

Solution 2:

If you get your data from a REST Service, you can simply convert your fields to Date.

$http.get(url).success(function(data){
     $scope.data = data; // get row data
     $scope.data.mydatefield = new Date($scope.data.mydatefield); // convert filed to date
});

Solution 3:

Create a simple directive that converts the model value:

HTML:

<input date-input type="time" ng-model="created_time">

Directive:

app.directive('dateInput', function(){
    return {
        restrict : 'A',
        scope : {
            ngModel : '='
        },
        link: function (scope) {
            if (scope.ngModel) scope.ngModel = new Date(scope.ngModel);
        }
    }
});

Solution 4:

In addition to PSL's answer. This is how to override angular 1.3+ requirements to be a Date object.

<input type="date" ng-model="book.date" date-format/>

app.directive('dateFormat', function() {
  return {
    require: 'ngModel',
    link: function(scope, element, attr, ngModelCtrl) {
      //Angular 1.3 insert a formater that force to set model to date object, otherwise throw exception.
      //Reset default angular formatters/parsers
      ngModelCtrl.$formatters.length = 0;
      ngModelCtrl.$parsers.length = 0;
    }
  };
});

It can be used with AngularFire $firebaseObject and works fine with $bindTo 3-way binding. No need to extend $firebaseObject service. It works in Ionic/cordova applications.

Working example on jsfiddle

Based on this answer