2

I'm trying to set today as the max date in date input. But every time i choose today in the date picker, the model value was always undefined. Has someone met the same problem like this?

html code:

<form name="myForm" class="item"> 
    <input type="date" ng-model="date" max="{{maxDateStr}}" name="dateInput">

    <p> date: {{date | date:'yyyy-MM-dd'}}</p>
    <p>myForm.$valid :  {{myForm.$valid}}</p>
    <p>myForm.dateInput.$error.max :  {{myForm.dateInput.$error.max}}</p>
 </form>

js code:

angular.module('angularApp',[])

.controller('MyCtrl', function($scope, $filter) {
    $scope.maxDateStr = $filter('date')(new Date(), 'yyyy-MM-dd');
    $scope.date = new Date();

});

my code on codepen

after debug in chrome, i found that when choose the max date, the value pass to NgModelController.$parsers had time e.g. Thu Jul 02 2015 16:54:00 GMT+0800 (CST). When run the max validator it returns false,

angular max validator code:

  var maxVal;
  ctrl.$validators.max = function(value) {
    return !isValidDate(value) || isUndefined(maxVal) || parseDate(value) <= maxVal;
  };

because the maxVal value was Thu Jul 02 2015 00:00:00, it returns false.

10
  • your codepen doesn't open. and also, try to put the code in question itself in brief. Commented Jul 2, 2015 at 8:14
  • edit your link -> remove space in starting and add : after http Commented Jul 2, 2015 at 8:15
  • Saw your code... it is working fine for me... what is the exact problem? Commented Jul 2, 2015 at 8:21
  • I'm so sorry for that. The link has changed and can open now Commented Jul 2, 2015 at 8:24
  • @RohitKumar But when i choose the max date, the model value was undefined Commented Jul 2, 2015 at 8:26

1 Answer 1

3

Replace your code with following

angular.module('angularApp',[])

.controller('MyCtrl', function($scope, $filter) {
    $scope.maxDateStr = $filter('date')(new Date(), 'yyyy-MM-dd');
    $scope.date = new Date();
});

with

angular.module('angularApp',[])

.controller('MyCtrl', function($scope, $filter) {
    $scope.maxDateStr = $filter('date')(new Date(), 'yyyy-MM-dd');
    $scope.date = new Date((new Date()).getFullYear(), (new Date()).getMonth(), (new Date()).getDate());
});

Note: It will create date with 00:00:00 time, so it should perfect working with max validation.

Sign up to request clarification or add additional context in comments.

Comments

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.