Reputation: 1061
I have two date fields, from and to date.If i change the from date to date will get changed , using on change function in date picker. i have reset button in my form, if i reset the values should get reset. but my problem is if i select the from date (i.e 5/02/17) and the to date will be (i.e 5/02/17) i will not allow the user to select the future date
$('#fromDate').datepicker({
format:'dd/mm/yyyy',
endDate: '+0d',
autoclose: true
}).on('changeDate', function (selected) {
var minDate = new Date(selected.date.valueOf());
$('#toDate').datepicker('setStartDate', minDate);
var endDateValue = $("#toDate").val();
var date = new Date(minDate);
var dd = date.getDate();
var mm = date.getMonth() + 1;
var yyyy = date.getFullYear()+543;
if (dd < 10) {
dd = '0' + dd;
}
if (mm < 10) {
mm = '0' + mm;
}
var startDate = dd + '/' + mm + '/' + yyyy;
console.log(yyyy+ "endDateValue "+endDateValue);
if(endDateValue === ""){
$("#toDate").val($.datepicker.formatDate('dd/mm/yy', new Date(startDate)));
}else if(endDateValue < startDate){
$("#toDate").val($.datepicker.formatDate('dd/mm/yy', new Date(startDate)));
}
});
$('#toDate').datepicker({
format:'dd/mm/yyyy',
endDate: '+0d',
autoclose: true,
todayHighlight:false
}).on('changeDate', function (selected) {
var minDate = new Date(selected.date.valueOf());
var endDateValue = $("#fromDate").val();
if(endDateValue === ""){
var date = new Date(minDate);
var dd = date.getDate();
var mm = date.getMonth() + 1;
var yyyy = date.getFullYear()+543;
if (dd < 10) {
dd = '0' + dd;
}
if (mm < 10) {
mm = '0' + mm;
}
var endDate = dd + '/' + mm + '/' + yyyy;
console.log(yyyy+ "endDate "+endDate);
$("#fromDate").val($.datepicker.formatDate('dd/mm/yy', new Date(endDate)));
}
});
i have used above code to change my to date as well as from date. if i click clear button
$("#clearButton").click(function(){
$('#toDate').datepicker('update','');
});
i wrote the above code. my problem is if i click clear button , i could not able to clear my To date. (i.e I have selected the from date and clicked on clear button the to date is still my from date. i could not go beyond that ). I have tried all the ways but could not able to resolve. help me out
Upvotes: 1
Views: 526
Reputation: 22323
Use setDate :
$('#toDate').datepicker('setDate', null);
OR
Redraw the date picker
$('#toDate').datepicker( "refresh" );
Upvotes: 2
Reputation: 5071
Try this code
$('#toDate').val('')
or
$('#toDate').datepicker("clearDates");
or
$('#toDate').val('').datepicker('update');
or
$('#toDate').datepicker({
clearBtn: true
});
Upvotes: 2