0

How could I disable the old dates from the present date in the datepicker using a function script? Thank you!

booknow.php

        <html>
        <body>
        <label for="shootdate">Desired Date:</label>
        <input required type="date" name="shootdate" id="shootdate" title="Choose your desired date"/>
        </body>
        </html>
Jake Ramirez
  • 35
  • 1
  • 1
  • 4

5 Answers5

6
<html>
    <body>
        <label for="shootdate">Desired Date:</label>
        <input required type="date" name="shootdate" id="shootdate" title="Choose your desired date" min="<?php echo date('Y-m-d'); ?>"/>
    </body>
</html>

You can do this using PHP itself. We can easily set minimum date to today, so that user cannot select previous dates.

Or, if you need it only using jquery, give a try on this

<html>
 <head>
  <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
    <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
    <script>
     $( function() {
       $( "#shootdate" ).datepicker({
        minDate: 0
       });
     });
    </script>
 </head>
    <body>
     <label for="shootdate">Desired Date:</label>
     <input required type="text" name="shootdate" id="shootdate" title="Choose your desired date" />
    </body>
</html>
1

You just simply have to set the datepicker option when initializing. The option minDate needs to be setup in order to do that.

Initialize you datepicker like this:

jQuery('#shootdate').datepicker({
         minDate: 0
});
Parantap Parashar
  • 1,930
  • 1
  • 14
  • 22
0

Try This:

<label for="from">From</label> <input type="text" id="from" name="from"/> 
<label for="to">to</label> <input type="text" id="shootdate" name="to"/>

var dateToday = new Date();
var dates = $("#from, #shootdate").datepicker({
defaultDate: "+1w",
changeMonth: true,
numberOfMonths: 3,
minDate: dateToday,
onSelect: function(selectedDate) {
    var option = this.id == "from" ? "minDate" : "maxDate",
        instance = $(this).data("datepicker"),
        date = $.datepicker.parseDate(instance.settings.dateFormat || $.datepicker._defaults.dateFormat, selectedDate, instance.settings);
    dates.not(this).datepicker("option", option, date);
}

});

savani sandip
  • 4,190
  • 1
  • 10
  • 8
  • Not working :( Where does the "FROM" and "TO" came from? I dont get it huhuhu :( – Jake Ramirez Feb 22 '17 at 06:22
  • I don't understand all the jQuery answers. The OP hasn't used a jQuery tag, hasn't mentioned in the OP and hasn't included it in the code. – RobG Feb 22 '17 at 08:11
0
<p>Date: <input type="text" id="datepicker" /></p>

 <script>
 $(function() {
 $( "#datepicker" ).datepicker({ minDate: 0});
 });
 </script>
0

Using an input type date, you can set the minimum date using the min attribute. However, not all browsers in use support input type date (see MDN support matrix), so you'll need a fallback.

function getISODate(){
  var d = new Date();
  return d.getFullYear() + '-' + 
          ('0' + (d.getMonth()+1)).slice(-2) + '-' +
          ('0' + d.getDate()).slice(-2);
}

window.onload = function() {
  document.getElementById('minToday').setAttribute('min',getISODate());
}
Select date (must be on or after 20-Feb-2017)<input type="date" min="2017-02-20">
<br>
Select date(must be on or after today)<input type="date" id="minToday">
RobG
  • 142,382
  • 31
  • 172
  • 209