I can use averages and multiply the difference in days by 5/7 (weekdays/total days in the week). But that won't be 100% correct. Hopefully it isn't too complex to get the correct answer.
If you aren't concerned with country-specific holidays, and simply need the number of Mon-Fri days between two dates, this can currently be achieved using Javascript and the SCRIPT formula function (2.7+).
See attached IOK file which includes the formula and provides a series of test cases.
(If, on the other hand, you needed to factor in public holidays, you would need a much more sophisticated solution, involving lookup files by country.)
SCRIPT( ` function workingDaysBetweenDates(startDate, endDate) {
// Validate input if (endDate < startDate) return 0;
// Calculate days between dates var millisecondsPerDay = 86400 * 1000; // Day in milliseconds startDate.setHours(0,0,0,1); // Start just after midnight endDate.setHours(23,59,59,999); // End just before midnight var diff = endDate - startDate; // Milliseconds between datetime objects var days = Math.ceil(diff / millisecondsPerDay);
// Subtract two weekend days for every week in between var weeks = Math.floor(days / 7); var days = days - (weeks * 2);
// Handle special cases var startDay = startDate.getDay(); var endDay = endDate.getDay();
// Remove weekend not previously removed. if (startDay - endDay > 1) days = days - 2;
// Remove start day if span starts on Sunday but ends before Saturday if (startDay == 0 && endDay != 6) days = days - 1
// Remove end day if span ends on Saturday but starts after Sunday if (endDay == 6 && startDay != 0) days = days - 1
return days; }
workingDaysBetweenDates(start, end) ;
`, 'start',[Start Date], 'end',[End Date])
I get problems with the javascript results for some reason. To demonstrate this, running the script below returns false!
SCRIPT( ` start < end `, 'start',TEXTTODATE('2012-08-01','yyyy-MM-dd'), 'end',TEXTTODATE('2012-08-23','yyyy-MM-dd'))
whereas the code below returns true:
SCRIPT( ` var start = new Date("August 01, 2012 00:00:01") var end = new Date("August 23, 2012 00:00:01")
start < end
`)
To get around this, pass the dates as text using date, and convert them in JS to dates using the code above:
SCRIPT( ` function workingDaysBetweenDates(startDate, endDate) {
// Validate input if (endDate < startDate) return 0;
// Calculate days between dates var millisecondsPerDay = 86400 * 1000; // Day in milliseconds startDate.setHours(0,0,0,1); // Start just after midnight endDate.setHours(23,59,59,999); // End just before midnight var diff = endDate - startDate; // Milliseconds between datetime objects var days = Math.ceil(diff / millisecondsPerDay);
// Subtract two weekend days for every week in between var weeks = Math.floor(days / 7); var days = days - (weeks * 2);
// Handle special cases var startDay = startDate.getDay(); var endDay = endDate.getDay();
// Remove weekend not previously removed. if (startDay - endDay > 1) days = days - 2;
// Remove start day if span starts on Sunday but ends before Saturday if (startDay == 0 && endDay != 6) days = days - 1
// Remove end day if span ends on Saturday but starts after Sunday if (endDay == 6 && startDay != 0) days = days - 1
return days; } var startd = new Date(start) var endd = new Date(end)