0

I have a start date and end date for me to choose the date. I am using jQuery datepicker library.

This is how my function looks like for now:

$(function() {

        $(".txtEndDate").datepicker({
            changeMonth: true,
            changeYear: true,
            showOn: 'button',
            buttonImage: '../../../images/calendar.png',
            buttonImageOnly: true,
            title:'Click to open calendar',
            alt:'Click to open calendar'
        });


    });




    $(function() {

        $(".txtStartDate").datepicker({
            changeMonth: true,
            changeYear: true,
            showOn: 'button',
            buttonImage: '../../../images/calendar.png',
            buttonImageOnly: true,

            title:'Click to open calendar',
            alt:'Click to open calendar',

            onSelect: function() {
                var start = $(this).datepicker("getDate");
                start.setDate(start.getDate() + 7);
                $(".txtEndDate").datepicker("setDate", start);
            }

        });



    });

It works but I want to make it when users are not allowed to choose a date before today because it doesn't make sense anyway.For example,choosing 2018-01-01 is not supposed to be valid. You get the idea.

Found a documentation that suggests using minDate to solve this.It says:

//initialize the datepicker
$( ".selector" ).datepicker({
  minDate: new Date(2007, 1 - 1, 1)
});

//get or set mindate option
// Getter
var minDate = $( ".selector" ).datepicker( "option", "minDate" );

// Setter
$( ".selector" ).datepicker( "option", "minDate", new Date(2007, 1 - 1, 1) );

Seeing as I am confused on what the fields represent, is there a way I could prevent people from selecting any dates before today?

I tried this solution: jQuery Date Picker - disable past dates

But it doesn't disable it and still allows me to choose.

Bathri Nathan
  • 1,101
  • 2
  • 13
  • 17
Daredevil
  • 1,672
  • 3
  • 18
  • 47

2 Answers2

1

You can disable past dates using minDate property of datepicker plugin, as below.

$(".txtEndDate").datepicker({
            minDate: new Date(),  // Add this to diсable past date
            changeMonth: true,
            changeYear: true,
            showOn: 'button',
            buttonImage: '../../../images/calendar.png',
            buttonImageOnly: true,
            title:'Click to open calendar',
            alt:'Click to open calendar'
        });
Daut
  • 2,537
  • 1
  • 19
  • 32
Abhishek
  • 972
  • 3
  • 12
  • 24
1

use the below code it will help you to prevent selecting old date from today.

minDate : 0 // for current date

minDate: new Date(2007, 1 - 1, 1) // for particular minDate 

$("input.DateFrom").datepicker({
    minDate: 0  
});
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
  <link rel="stylesheet" href="/resources/demos/style.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>
<input class="DateFrom">

code to end date less that start date please refer the code :-

  $("#StartDate").datepicker({
        numberOfMonths: 2,
        onSelect: function(selected) {
          $("#EndDate").datepicker("option","minDate", selected)
        }
    });
    $("#EndDate").datepicker({ 
        numberOfMonths: 2,
        onSelect: function(selected) {
           $("#StartDate").datepicker("option","maxDate", selected)
        }
    });  
Bathri Nathan
  • 1,101
  • 2
  • 13
  • 17