0

I am trying to add a date to an input but seem to be having real problems with the errors even tho the date is displaying

Formatt code

    $scope.formatt=function(dte){

return moment(dte).format("YYYY-MM-DD");

}

Html Code:

<input id="userName" value="{{formatt(contents.PaymentDate)}}" ng-model="contents.PaymentDate" name="username" type="date" class="form-control">

Error:

Error: [ngModel:datefmt] http://errors.angularjs.org/1.5.0/ngModel/datefmt?p0=2017-03-21%2000%3A00%3A00
at angular.js:38
at Array.<anonymous> (angular.js:22838)
at angular.js:26467
at m.$digest (angular.js:16655)
at m.$apply (angular.js:16928)
at g (angular.js:11266)
at t (angular.js:11464)
at XMLHttpRequest.u.onload (angular.js:11405)

It is displaying the date fine but i am still getting the errors, any help would be appreciated

Thank you

3
  • 1
    Are you by any chance getting the data asynchronously? That means at first the value will be null so it will throw errors. After it is retrieved it will succesfully parse it to a date. Commented Feb 10, 2017 at 10:12
  • Actually ngModel expect contents.PaymentDate to be a date as you can see on the error url given on your question https://docs.angularjs.org/error/ngModel/datefmt?p0=2017-03-21%2000:00:00 Commented Feb 10, 2017 at 10:14
  • Yes i am getting the data asynchronously Commented Feb 10, 2017 at 10:51

1 Answer 1

4

The issue is coming because ng-model is not able to detect Date object. so convert contents.PaymentDate to date object like as

Controller.js

$scope.contents.PaymentDate = new Date($scope.contents.PaymentDate); 
Sign up to request clarification or add additional context in comments.

2 Comments

Hi i understand this is the problem but im getting 'paymentdate' is undefined
can you let me know the paymentDate coming from any api request or you just set as new variable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.