Possible days in a date range
It gives you facility to check, how many days are possible inside a date range
by Raghvendra Singh
HTML
<link rel="stylesheet" href="https://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<script src="https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.12.0/moment.min.js"></script>
<p>Start Date: <input type="text" id="start-date"></p>
<p>End Date: <input type="text" id="end-date"></p>
<button id ="btn">
Get Possible Days
</button>
<div id="output">
</div>
CSS
#output {
margin-top: 20px;
}
JavaScript
$(function() {
$( "#start-date" ).datepicker();
$( "#end-date").datepicker();
});
$("#btn").click(function(){
var days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
if (($("#start-date").val() === "") || $("#end-date").val() === "") {
$("#output").html("Select both start date and end date");
} else {
var startDate = moment($("#start-date").val());
var endDate = moment($("#end-date").val());
if (endDate < startDate) {
$("#output").html("End date must be greater than or equal to start date.");
} else {
if (endDate.diff(startDate, 'days') >= 7) {
$("#output").html("<b>Possible Days: </b>" + days.join(", "));
} else {
var possibleDays = [];
var iDate = startDate;
while (iDate <= endDate) {
possibleDays.push(days[iDate.day()]);
iDate = iDate.add(1, 'days');
}
$("#output").html("<b>Possible Days: </b>" + possibleDays.join(", "));
}
}
}
});