I am in the process of developing a system for scheduled deliveries.
I have written a script that calculates the quantity of specific days of the week between two dates in dd/mm/yyyy format (UK).
var date0 = "01/02/2021";
var date1 = "31/01/2022";
var dayList = [1]; // Monday based on Sunday being 0
var result = countDays(dayList, parseDate(date0), parseDate(date1));
alert(result);
function parseDate(str) {
var dmy = str.split('/');
return new Date(+dmy[2], dmy[1] - 1, +dmy[0]);
}
function countDays(days, fromDate, toDate) {
var ndays = 1 + Math.round((toDate-fromDate)/(24*3600*1000));
var sum = function(a, b) {
return a + Math.floor((ndays + (fromDate.getDay() + 6 - b) % 7) / 7);
};
return days.reduce(sum, 0);
}
In this case, when dayList=[1]
, it denotes 'Monday'. The script determines the total number of Mondays within the specified period and outputs the result to be 53 (since 01/02/2021 falls on a Monday).
This method is effective for tracking weekly occurrences of a particular day within a rolling week timeframe.
However, I am seeking guidance on modifying this approach to accommodate a rolling fortnight schedule.
For instance, I would like to calculate the frequency of both weekly Mondays and alternate Fridays within a rolling fortnight, rather than just a rolling week. This necessitates handling a combination of weekly and bi-weekly delivery schedules.
Consider this scenario: the client requests a shipment every Monday along with an additional delivery every other Friday.
Sat Sun Mon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri
x x x
Delivery schedules can vary significantly, with the rolling fortnight commencing on the user-provided start date.
The main challenge lies in determining the total number of days between the start and end dates while incorporating the concept of a 'rolling fortnight' into the calculation.