问题
I am using md-datepicker, when i put date manually it is showing invalid date but if i select date from this control then it is validated. for this i have used following code
<md-datepicker name="FromDate" ng-model="vm.user.FromDate" ng-required="true" id="FromDate" md-is-error="login.ToDate.$invalid && login..$submitted" md-placeholder="FromDate"></md-datepicker>
<div ng-messages="login.FromDate.$error" md-auto-hide="false" ng-show="login.FromDate.$touched ||login.FromDate.$submitted">
<div ng-message="required">Please enter FromDate.</div> </div>
回答1:
i found the answer. in config section along with formatDate i added parseDate function and it is working fine no error while manual entry as well as selection from control
.config(function ($mdDateLocaleProvider) {
$mdDateLocaleProvider.formatDate = function (date) {
return date ? moment(date).format('DD-MM-YYYY') : '';
};
$mdDateLocaleProvider.parseDate = function (dateString) {
var m = moment(dateString, 'DD-MM-YYYY', true);
return m.isValid() ? m.toDate() : new Date(NaN);
};
});
回答2:
please check the format of the date that you are entering the default format is mm/dd/yyyy
来源:https://stackoverflow.com/questions/44432802/md-datepicker-shows-error-when-date-entered-manually