As a newcomer to web programming, I am currently working on calculating the number of days between two dates while excluding weekends and public holidays specific to our country for each year.
I am looking to create a function that allows me to input a year, such as 2021 or 2022, and receive a list of all the public holidays for that year. Additionally, I have already used JavaScript to calculate the date difference while excluding weekends.
The code snippet below showcases the calculation of the date difference excluding weekends:
function getBusinessDateCount (startDate, endDate) {
var elapsed, daysBeforeFirstSaturday, daysAfterLastSunday;
var ifThen = function (a, b, c) {
return a == b ? c : a;
};
elapsed = endDate - startDate; elapsed /= 86400000;
daysBeforeFirstSunday = (7 - startDate.getDay()) % 7; daysAfterLastSunday = endDate.getDay();
elapsed -= (daysBeforeFirstSunday + daysAfterLastSunday);
elapsed = (elapsed / 7) * 5;
elapsed += ifThen(daysBeforeFirstSunday - 1, -1, 0) + ifThen(daysAfterLastSunday, 6, 5);
return Math.ceil(elapsed); }
$(document).ready(function(){
$(document).on('change','#date2',function (evt) {
let start = document.querySelector('#date1').value,
end = document.querySelector('#date2').value,
result = getBusinessDateCount(new Date(start),
new Date(end));
document.getElementById("days").value = result+ " Days";
}); });
Currently, I am facing challenges in incorporating public holidays into this calculation to determine the number of days between two dates while excluding both weekends and public holidays specific to our country.
If you have any suggestions or ideas on how to achieve this, please lend a helping hand.