Sean
Sean

Reputation: 15144

Angular UI datepicker - date format from API

I'm using the Angular UI datepicker component. When my data comes down from the API, I get dates in this format in the JSON:

"date": "2015-10-21T00:00:00"

The datepicker is correctly formatting the date in the input field to "21 October 2015". But, if I click the submit button on the form, without making any changes, the date field fails validation.

If I set the date model to a date object (as opposed to a string) like so, after the JSON loads:

vm.date = new Date();

Then the validation works.

So it seems the datepicker component doesn't set the date correctly, when using a string value as the model.

Any way to fix this?

Here is a link to a plunkr: http://plnkr.co/edit/gAhme7fmIPgbojYVJlNU?p=preview

If you choose a date using the datepicker calender, it works fine. But if the date gets set using the button, as it would using JSON API data, then it fails validation.

Upvotes: 0

Views: 1494

Answers (1)

Michael
Michael

Reputation: 3104

Looking into the source code it seems that the model date can be set as timestamp (number) or as date object, but NOT as string. A string is expected as an input value and will be parsed with your custom data format dd MMMM yyyy.

See datepicker.js 780-800

Upvotes: 0

Related Questions