NARTONIC
NARTONIC

Reputation: 472

Set minDate in Jquery

I want how to set minDate. I use this

$(function () {
    $("#dph-entry").datepicker('option', 'minDate', new Date(2017, 3, 27));
});

the input

<input type='text' class="con-input" id='dph-entry' placeholder="Entrada"/>

but doesn't works

Upvotes: 0

Views: 3565

Answers (2)

Ionut Necula
Ionut Necula

Reputation: 11462

The way you use it is how you can set the min date after initialization.

Get or set the minDate option, after initialization:

// Getter

var minDate = $( ".selector" ).datepicker( "option", "minDate" );

// Setter

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

When you are trying to set the default min date it means you are trying to init the datepicker with min date, that's why you need to add the options a bit differently, property: value:

Initialize the datepicker with the minDate option specified:

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

See the working snippet below please:

$(function() {
  $(document).ready(function() {
    //$("#dp-entry").datepicker('option', 'minDate', new Date(2017, 3, 27));
    $("#dph-entry").datepicker({minDate: new Date(2017, 3, 27)});
  })
});
<link href="https://code.jquery.com/ui/jquery-ui-git.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>
<input type='text' class="con-input" id='dph-entry' placeholder="Entrada" />

You can read about what I said here.

Upvotes: 1

Satpal
Satpal

Reputation: 133403

You need to initialize datepicker() first. Then only option's can be set.

$("#dph-entry").datepicker();
$("#dph-entry").datepicker('option', 'minDate', new Date(2017, 3, 27));

Better pass options while initializing

$("#dph-entry").datepicker({'minDate': new Date(2017, 3, 27)});

$(function() {
  $("#dph-entry").datepicker({
    'minDate': new Date(2017, 3, 27)
  });
});
<link href="https://code.jquery.com/ui/jquery-ui-git.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>
<input type='text' class="con-input" id='dph-entry' placeholder="Entrada" />

Upvotes: 1

Related Questions