I guys i'm using a datetimepicker by trentrichardson.com.
I have a form with two input fields : from and to and i want to be able to set dynamically a minDate to my "to" field equal to the value of my "from" field.
I know i should use a beforShow option but don't know how to implement the function.
$('.from,.to').datetimepicker({
beforeShow: customRange
});
EDITED/SOLUTION
function customRange(input) {
if (input.id == 'to') {
var x=$('#from').datepicker("getDate");
$( ".to" ).datepicker( "option", "minDate",x );
}
else if (input.id == 'from') {
var x=$('#to').datepicker("getDate");
$( ".from" ).datepicker( "option", "maxDate",x );
} }
$(function() { var date = new Date(); $("#Panel2nrFecha"). datepicker({ minDate: date, maxDate: new Date($("#Panel2nrTextBox1"). val()), dateFormat: "dd/mm/y", defaultDate: "+1w", changeMonth: true, numberOfMonths: 3 }); });
To set date range of one month from current date, we will use 'M' option to "+1" for maxDate and for minDate "0" So the current date becomes the value of minDate. See below jQuery code. $(document). ready(function(){ $("#txtDate").
If you like to restrict access of users to select a date within a range then there is minDate and maxDate options are available in jQuery UI. Using this you can set the date range of the Datepicker. After defining these options the other days will be disabled which are not in a defined range.
To set it after init, use $('#datepicker'). datepicker( "option", "minDate", new Date( 1999, 10 - 1, 25 ) ) .
you can set and get minDate dynamically with setter and getter:
//getter
var minDate = $( ".selector" ).datepicker( "option", "minDate" );
//setter
$( ".selector" ).datepicker( "option", "minDate", new Date(2007, 1 - 1, 1) );
explained here
If you have two textboxes with corresponding IDs from
and to
and you want the minDate
of to
to be the selected date of from
then do the following:
$("#from").datepicker({
minDate: 0, // to disable past dates (skip if not needed)
onClose: function(selectedDate) {
// Set the minDate of 'to' as the selectedDate of 'from'
$("#to").datepicker("option", "minDate", selectedDate);
}
});
$("#to").datepicker();
You can restrict the datepicker range.
But you want to set this range on creation of the datepicker on the "from". I made you a demo which seems to work OK as far as I understand the question.
luca: A small addition to your answer...
Using the function you suggested the time component is ignored the first time the datetimepicker is displayed. If you close the datetimepicker and reopen it again, the time component mysteriously reappears. I'm not quite sure what is causing this, but it can be fixed with a bit of a hack:
function setMinMaxDate ( element )
{
// Store current date - setting max/min date seems to destroy time component
var val = $(element).val();
if (element.id == 'endDate')
{
var x=$('#startDate').datetimepicker("getDate");
$( element ).datetimepicker( "option", "minDate",x );
}
else if (element.id == 'startDate')
{
var x=$('#endDate').datetimepicker("getDate");
$( element ).datetimepicker( "option", "maxDate",x );
}
// Restore date
$(element).val(val);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With