User123
User123

Reputation: 461

Input type date ng-model does not allow date data

From the server response, i want to map the date to my scope variable which type is 'date' in angular, as i do the same, am not able to map it as am getting the following error

angular.js:12798 Error: [ngModel:datefmt] Expected `2016-09-22T18:30:00.000Z` to be a date
http://errors.angularjs.org/1.4.12/ngModel/datefmt?p0=2016-09-22T18%3A30%3A00.0

Note: am mapping through $operatingDetails.date as exceptions array already in the ng-repeat loop

Any help please would be appreciated...

Html

 <div ng-repeat="operatingDetails in operatingDetails.exceptions" style="margin-top: 8px;">
             <div class="DateField">
             <input id="date2" type="date" ng-model="operatingDetails.date" style="" ng-change="customOPHchangeExpDateValid(operatingDetails.date, $parent.$parent.$index)"/>
             </div>
</div>

Js(Angular)

$scope.opertingHours.date

Server response

{
                "name" : "Custom Operation Hours1",
                "exceptions" : [{
                        "date" : "2016-09-15T18:30:00.000Z",
                        "starttime" : "10:02AM",
                        "endtime" : "10:02PM",
                        "_id" : "57de44c54feb409c2e13ff40",
                        "$$hashKey" : "object:207"
                    }
                ],
                "businesshour" : {
                    "sat" : {
                        "starttime" : "8:00AM",
                        "endtime" : "5:00PM"
                    },
                    "fri" : {

Upvotes: 1

Views: 789

Answers (2)

Aravind
Aravind

Reputation: 41571

your operatingDetails is getting confused as you are using it in the $scope and in ng-repeat so try using another one as below

<div ng-repeat="oD in operatingDetails.exceptions" style="margin-top: 8px;">
                 <div class="DateField">
                 <input id="date2" type="date" ng-model="convertDate(oD.date)" style="" ng-change="customOPHchangeExpDateValid(oD.date, $parent.$parent.$index)"/>
                 </div>
</div>

Also add the following function to your controller

$scope.convertDate=function(inputDate){
return new Date(inputDate);
}

Upvotes: 0

Umakanta Behera
Umakanta Behera

Reputation: 265

I guess you are getting this error, as "date" ("date" : "2016-09-15T18:30:00.000Z",) is a string.So convert it to date from string.

So try this,

$scope.opertingHours.date = new Date(exceptions[0].date); //Converting string to date

Hope,it wll work for u.

Upvotes: 2

Related Questions