How to disable past date in daterangepicker?
Asked Answered
E

3

8

I using two date selected daterangepicker. this working perfect but how to disable past date. below is my code

js/site/daterange/moment.min.js">
    <script type="text/javascript" src="<?php echo base_url();?>js/site/daterange/daterangepicker.js"></script>
    <link rel="stylesheet" type="text/css" href="<?php echo base_url();?>css/site/daterangepicker.css" />
    <script type="text/javascript">
      $(function() {
      $('input[name="checkin"],input[name="checkout"]').daterangepicker({
          autoUpdateInput: false,
          locale: {
              cancelLabel: 'Clear'
          }
      });

      $('input[name="checkin"],input[name="checkout"]').on('apply.daterangepicker', function(ev, picker) {
          //$(this).val(picker.startDate.format('MM/DD/YYYY') + ' - ' + picker.endDate.format('MM/DD/YYYY'));
          $('#checkin').val(picker.startDate.format('MM/DD/YYYY'));
          $('#checkout').val(picker.endDate.format('MM/DD/YYYY'));

      });

      $('input[name="checkin"],input[name="checkout"]').on('cancel.daterangepicker', function(ev, picker) {
          $(this).val('');
      });

      });

Envelop answered 21/6, 2016 at 13:48 Comment(0)
P
11

I had the same issue. I checked http://www.daterangepicker.com/#options and seems to me minDate would do the job.

    var today = new Date(); 
    var dd = today.getDate(); 
    var mm = today.getMonth()+1; //January is 0! 
    var yyyy = today.getFullYear(); 
    if(dd<10){ dd='0'+dd } 
    if(mm<10){ mm='0'+mm } 
    var today = dd+'/'+mm+'/'+yyyy; 
    $('input[name="daterange"]').daterangepicker({

             minDate:today
    });
Pendent answered 8/3, 2017 at 10:19 Comment(0)
S
15

this is easy way to solve the problem

$('input[name="daterange"]').daterangepicker({
      minDate:new Date()
});
Stratfordonavon answered 30/10, 2019 at 12:10 Comment(0)
P
11

I had the same issue. I checked http://www.daterangepicker.com/#options and seems to me minDate would do the job.

    var today = new Date(); 
    var dd = today.getDate(); 
    var mm = today.getMonth()+1; //January is 0! 
    var yyyy = today.getFullYear(); 
    if(dd<10){ dd='0'+dd } 
    if(mm<10){ mm='0'+mm } 
    var today = dd+'/'+mm+'/'+yyyy; 
    $('input[name="daterange"]').daterangepicker({

             minDate:today
    });
Pendent answered 8/3, 2017 at 10:19 Comment(0)
O
1

So as far as i can see from your code you want to disable dates that are in the past so there are multiple ways to do such a thing but the easiest of them in my opinion would be to get the current date on document load and set that as the start date for your date range picker.

http://www.daterangepicker.com/#options should give you the example explaining the startDate syntax to do the same and the code to find the current date in the said format can be show as below:

var today = new Date(); 
var dd = today.getDate(); 
var mm = today.getMonth()+1; //January is 0! 
var yyyy = today.getFullYear(); 
if(dd<10){ dd='0'+dd } 
if(mm<10){ mm='0'+mm } 
var today = dd+'/'+mm+'/'+yyyy; 

Here today stores the string form of the format you need and can be set to the startDate attribute.

Open answered 21/6, 2016 at 14:1 Comment(2)
For us U.S. people, watch the order of the date. We need var today = mm+'/'+dd+'/'+yyyy; I was staring at this for like 30 minutes before it clicked.Patisserie
Hey dear @BanelingRush, I hope before writing this answer you have checked all the other answers to this question. If not please check and provide something new to the question.Crites

© 2022 - 2024 — McMap. All rights reserved.