Set min date to current date in angularJS input

前端 未结 5 781
梦如初夏
梦如初夏 2020-12-30 02:59

In AngularJS, how can we set the min attribute of input type=\"date\" to current date (today\'s) ?



        
5条回答
  •  遥遥无期
    2020-12-30 03:27

    According to the Angular docs

    min (optional) string Sets the min validation error key if the value entered is less than min. This must be a valid ISO date string (yyyy-MM-dd).

    you can bind that to a scope variable or directly put the expression in the attribute, it just needs to be in the correct format (javascript date object has a toISOString() method). It should also be noted this does not prevent the user from picking a date below the minimum and instead sets the validation key to indicate the field is not valid.

    edit:

    Script

    var app = angular.module('plunker', []);
    
    app.controller('MainCtrl', function($scope) {
      $scope.name = 'World';
      var today=new Date();
      $scope.today = today.toISOString();
    });
    

    Html

    
    
    
      
        
        AngularJS Plunker
        
        
        
        
      
    
      
        

    Hello {{name}}!

    Start Date is required Start Date should not be less than current date

    your error message indicates you want the date to be no greater than the current date, if that is the case simply change

      
    

    to

      
    

    Working Example

提交回复
热议问题