From
<br>
<input type="text" id="from" class="datepicker" name="from" />
<br>
<br>To (should be +4 days from the above field)
<br>
<input type="text" id="to" class="datepicker" name="to" />
<br>
<br>This is calculation of total days
<br>
<input type="text" id="totaldays" name="days" />
<br>
<br>
JavaScript
$(function () {
$("#from").datepicker({
defaultDate: "+1w",
changeMonth: true,
numberOfMonths: 2,
minDate: 4,
onSelect: function (selectedDate) {
var fromDate = new Date(selectedDate);
var maxDate = new Date(fromDate.setDate(fromDate.getDate() + 4));
//$('#to').datepicker('option', 'maxDate', maxDate);
$("#to").datepicker("option", "minDate", maxDate);
},
onClose: function (selectedDate) {
totalDays();
}
});
$("#to").datepicker({
defaultDate: "+1w",
changeMonth: true,
numberOfMonths: 2,
onSelect: function(selectedDate) {
//same as #from
//var toDate = new Date(selectedDate);
//var minDate = new Date(toDate.setDate(toDate.getDate() - 4));
//$('#from').datepicker('option', 'minDate', minDate);
//$("#from").datepicker("option", "maxDate", selectedDate);
},
onClose: function (selectedDate) {
totalDays();
}
});
function totalDays() {
var from = $('#from').datepicker('getDate');
var to = $('#to').datepicker('getDate');
var seconds = to - from;
var days = Math.ceil(seconds / (1000 * 3600 * 24));
if (days >= 0 && from) {
$('#totaldays').val(days);
}
}
});
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.