I would like to exclude the weekends or the disabled days in calculating the selected days.
how can i do that, I’m using Datepicker bootstrap also my framework is laravel.
<div class="input-group input-date range">
<input type="text" class="form-control" id="from" value=""/>
<div class="input-group-addon">to</div>
<input type="text" class="form-control" id="to" value=""/>
</div>
<div class="form-group">
<label>Number of days</label>
<input class="form-control" readonly type="text" id="numberdays" name="day">
</div>
this is my script
<script>
$(document).on('click',function()
{
$('#from').datepicker({
dateFormat: 'mm/dd/yyyy',
startDate: '-0',
showOnFocus: true,
todayHighlight: true,
autoclose: true,
daysOfWeekDisabled: '0,6',
orientation: 'bottom'
}).on('changeDate', function (eve) {
});
$('#to').datepicker({
dateFormat: 'mm/dd/yyyy',
startDate: '-0',
showOnFocus: true,
todayHighlight: true,
daysOfWeekDisabled: '0,6',
autoclose: true,
orientation: 'bottom'
}).on('changeDate', function (ev) {
var start = $("#from").val();
var startD = new Date(start);
var end = $("#to").val();
var endD = new Date(end);
var curDate = new Date(start);
var diff = parseInt((endD.getTime()-startD.getTime())/(24*3600*1000) + 1);
$("#numberdays").val(diff);
});
});
</script>
2
Answers
Something like this +/- a day depending on from – to is including both
I made a function to exclude the date index. Loop through from start to end to check the index, then exclude it from the summary if it’s disabled.
$("#numberdays").val(calcDaysExclude(startD, endD, [0, 6]));
Here the index of Saturday is 6, and Sunday is 0