Formatter wrong operand to process JSON dates
Hi,
When trying to assign JSON dates to ng-model value of a mdDatePicker, it doesn't seem to be working. I could find a formatter that I think is trying to achieve this functionality, but it looks wrong.
Currently it stands as
ngModel.$formatters.unshift(function(value) { var date = angular.isDate(value) && moment(value); if(date && date.isValid()) updateInputElement(date.format(scope.dateFormat)); else updateInputElement(null); });
But it doesn't make much sense to me, if value is not a date, it will always be FALSE without evaluating moment, so I think this should be:
ngModel.$formatters.unshift(function(value) { var date = angular.isDate(value) || moment(value); if(date && date.isValid()) updateInputElement(date.format(scope.dateFormat)); else updateInputElement(null); });
Am I missing something obvious here?
I will submit a pull request with this change, just in case.
Thanks,
Hi @pedromarce, you should use a Date object in your model
Hi,
I understand that if I use a Date object in my model, then it would work, but I am fetching all my model by a rest call, and would like to avoid having to convert every date in every possible model into a Date object. Besides, what is the purpose of the call to moment(value) in the formatter as it stands right now? If value is already a date we don't need moment (and it is being executed), if it is not, and moment could fix it, we never execute it. Just for your information with my change, seems to work fine.
Cheers,
Because a Date object could be also an invalid date object for example
angular.isDate(new Date("foo")) // this returns true
I see, that makes sense, but in that case, wouldn't that code be redundant? And with only moment(value) would achieve same functionality?
That would also solve my use case where I need to use the ng-model with an ISO 8601 date?
Or do you think that would have undesired side effects?
Cheers,
And with my attr date is a moment object? could you check first with ngModel already is moment because angular.isDate(moment()) is false.