Search code examples
javascriptjquerydatepickermindate

minDate: 0, not working in datepicker


I'm using a AdminLte template for my CodeIgniter application, and from that I'm using a Datepicker plugin. I'm trying to add minDate functionality to restrict user to access previous date from current date, but the minDate: 0 is not working and I'm able to pick previous date also.

Here is my JS:

$('#datepicker5').datepicker({
      autoclose: true,
      //changeYear: true,
      minDate: 0,
    });

and

$('#datepicker5').datepicker({
      autoclose: true,
      //changeYear: true,
      minDate: new Date(),
    })

Today's date is 12 Dec. 2017 but I can select previous date also. enter image description here Both are not working.

I'm using two date pickers with two different ID's. One is datepicker4 and datepicker5, and I'm calculating diff. between them. For that I've written following JS:

$('#datepicker4').datepicker();
  $('#datepicker5').datepicker();
  $('#datepicker5').change(function () {
      var gasDiff= $('#datepicker4').datepicker("getDate") - $('#datepicker5').datepicker("getDate");
      var result = gasDiff / (1000 * 60 * 60 * 24) * -1;

      document.getElementById("gasDiff").value = result;
  });

Even I can able to select expiry date before issue date. Actually I want to pick issue date before or current date, and expiry date after current date.

Edit:

$('#datepicker1').datepicker({
    autoclose: true,
    endDate: new Date(),
    todayHighlight: true,

  });

  $("#datepicker1").on("change",function(){
        var selected = "Are you sure you want to select this date ?";
        alert(selected);
    });
  //$('#datepicker1')..inputmask('dd/mm/yyyy', { 'placeholder': 'dd/mm/yyyy' });

  $('#datepicker2').datepicker({
    autoclose: true,
    todayHighlight: true,
    startDate: new Date(),
  });

  $('#datepicker1', '#datepicker2').change(function(){
    var certDiff= $('#datepicker1').datepicker("getDate") - $('#datepicker2').datepicker("getDate");    
    var result = certDiff / (1000 * 60 * 60 * 24) * -1;
    //("#certDiff").text(result);
    document.getElementById("certDiff").value = result;
    //document.getElementById("certDiff").innerHTML = result;
  });

Please any kind of help is welcome, Thanks in advance.


Solution

  • As described bootstrap-datepicker plugin is working with startDate: new Date() attribute

    $('#datepicker4').datepicker({
    autoclose: true,
    startDate: new Date() 
    });
    
    $('#datepicker5').datepicker({
     autoclose: true,
     startDate: new Date(),
    }).change(function(){
    var gasDiff= $('#datepicker4').datepicker("getDate") - $(this).datepicker("getDate");    
    var result = gasDiff / (1000 * 60 * 60 * 24) * -1;
      $("#gasDiff").text(result);
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.7.1/js/bootstrap-datepicker.js"></script>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.7.1/css/bootstrap-datepicker.css" rel="stylesheet"/>
    
    <input type="text" id="datepicker4"/>
    <input type="text" id="datepicker5"/>
    <div id="gasDiff">
    
    </div>