I am trying to compute number of days between a range of dates input by the user using date picker using the following script:
$(document).ready(function () {
$('#datepicker1').datepicker();
$('#datepicker2').datepicker();
$('#datepicker3').datepicker();
$('#datepicker4').datepicker();
$(function() {
let $fromDate = $('#fromdate'),
$toDate = $('#todate'),
$numberDays = $('#leavedays'),
$sfromDate=$('#sfromdate'),
$stoDate=$('#stodate'),
$snumberDays=$('#sleavedays');
$fromDate.datepicker().on('change', function(){
$toDate.datepicker('option', 'minDate', $(this).val());
$numberDays.val(calculateDateDiff($toDate.val(), $(this).val())+1);
});
$toDate.datepicker().on('change', function(){
$fromDate.datepicker('option', 'maxDate', $(this).val());
$numberDays.val(calculateDateDiff($(this).val(), $fromDate.val())+1);
});
$sfromDate.datepicker().on('change', function(){
$stoDate.datepicker('option', 'minDate', $(this).val());
$snumberDays.val(calculateDateDiff($stoDate.val(), $(this).val())+1);
});
$stoDate.datepicker().on('change', function(){
$sfromDate.datepicker('option', 'maxDate', $(this).val());
$snumberDays.val(calculateDateDiff($(this).val(), $sfromDate.val())+1);
});
function calculateDateDiff(endDate, startDate) {
if (endDate && startDate) {
let e = moment(endDate),
s = moment(startDate);
return e.diff(s, "days");
}
return null;
}
});
});
The script is able to return the correct difference between from date and to date, but I want to exclude weekend while computing the difference. Please help how to proceed for the same
2
Answers
An easy way is to adjust the input arguments to be the first/last day of the week, then divide by 7 to determine how many weekends in between those dates that you need to remove:
Fiddle: https://jsfiddle.net/k3uxeo12/3/
Having a quick search you can find other very similar posts:
Not DatePicker related but in terms of time, I only get the value of the date and send it to the server. This is what I did using PHP,
I found the method somewhere and modified it a little bit. It will output the following:
…and so on. I think it’s easier to apply only restrictions to the frontend but the real logic was on the backend… sorry for my english