0

I am trying to disable previous dates in calendar. I am using this code like

My HTML code is below.

<input type="text" required name="date_from" name="date_from" class="mydate input-text full-width" placeholder="Departure Date" />

My script code.

<script type="text/javascript">
 $(".mydate").datepicker({
       format:'yyyy-mm-dd',
       autoclose: true
   });
    $(".flexslider").flexslider({
        animation: "fade",
        controlNav: false,
        animationLoop: true,
        directionNav: false,
        slideshow: true,
        slideshowSpeed: 5000
    });
</script>

It shows calendar without previous dates disabled.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
nirvi
  • 9
  • 1
  • 1
  • 2

7 Answers7

1

Try this:

$( ".mydate" ).datepicker({ minDate: 0});
Bits Please
  • 877
  • 6
  • 23
1

Use bootstrap date picker. These are files to include

<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.3.0/css/datepicker.css" rel="stylesheet" type="text/css" />

<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.3.0/js/bootstrap-datepicker.js"></script>

Use this code to disable previous dates

HTML Input field

<input id="date" data-provide="datepicker" name="date_from" >

JAVASCRIPT

var date = new Date();
date.setDate(date.getDate());

$('#date').datepicker({ 
startDate: date
});
Danish Ali
  • 2,354
  • 3
  • 15
  • 26
0

Set minDate in initialization on datepicker

minDate:new Date()


$(".mydate").datepicker({
       format:'yyyy-mm-dd',
       autoclose: true,
       minDate:new Date()
   });
Bits Please
  • 877
  • 6
  • 23
Nirali
  • 1,776
  • 2
  • 12
  • 23
0

You have to set the minDate option. Example + fiddle on this forum:

jQuery Date Picker - disable past dates

Ties Vandyke
  • 154
  • 1
  • 8
0

You need to set minDate option while applying datepicker.

Try This,

<script type="text/javascript">
$(".mydate").datepicker({
       format:'yyyy-mm-dd',
       autoclose: true,
       minDate: 0,   
}); 
</script>
Jaydip Shingala
  • 429
  • 2
  • 11
  • 18
0

As from your format may be you are using bootstrap date picker. So use startDate to disable previous dates

$(".mydate").datepicker({
       format:'yyyy-mm-dd',
       startDate: new Date(),
       autoclose: true
   });
B. Desai
  • 16,414
  • 5
  • 26
  • 47
0

Try this code... this will restrict the user if user submits form with date less than today's date then shows alert message to change the date.

HTML Code

    <form name="myform" onsubmit="return validateDateOfAppointment()">
    <input type="date" name="Date of Appointment" placeholder="Date of Appointment" id="Date" />
</form>

JavaScript Code

function validateDateOfAppointment(){
        var date=document.getElementById("Date").value;
        var d=new Date();
        var x=d.getFullYear()+"-"+(d.getMonth()+1)+"-"+d.getDate();
        var checkDate=date.substr(8,2);
        var equalDate=d.getDate();
        var checkMonth=date.substr(5,2);
        var equalMonth=d.getMonth();
        var checkYear=date.substr(0,4);
        var equalYear=d.getFullYear();
        if(checkMonth>=equalMonth){
            if(checkDate<equalDate){
            alert("Date cannot be less than today!! ");
            return false;
            }
        }
        else if(checkMonth<equalMonth){
            if(checkYear<equalYear){
                alert("Date cannot be less than today!! ");
                return false;
            }
        }
     }
  • 1) why not just new Date("date from input").getTime() - new Date().getTime() ? 2) you should use onchange even or something like that ? – phoenixstudio Dec 12 '20 at 12:47
  • I used this code in a form submission where the user has to enter only present date and future dates only.......Now i added some more code for giving more clarification.. I used "onsubmit" for checking correct date during form submission. for 1) we get the date from user as a string so we cannot retrieve the time or date directly from that so I used string methods for getting date,month and year – Pranay Teja Dec 13 '20 at 06:31