mdPickers icon indicating copy to clipboard operation
mdPickers copied to clipboard

Formatter wrong operand to process JSON dates

Open pedromarce opened this issue 9 years ago • 5 comments

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,

pedromarce avatar May 18 '16 15:05 pedromarce

Hi @pedromarce, you should use a Date object in your model

alenaksu avatar May 18 '16 16:05 alenaksu

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,

pedromarce avatar May 18 '16 16:05 pedromarce

Because a Date object could be also an invalid date object for example

angular.isDate(new Date("foo")) // this returns true

alenaksu avatar May 18 '16 16:05 alenaksu

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,

pedromarce avatar May 18 '16 16:05 pedromarce

And with my attr date is a moment object? could you check first with ngModel already is moment because angular.isDate(moment()) is false.

taos-thiagoaos avatar Sep 15 '16 19:09 taos-thiagoaos