So, I have two dates, which come from the UI:
var start = new Date(self.Start()), end = new Date(self.End());
We need to figure out the number of days between that range, which is easy enough:
var days = Math.max(0, Math.floor((end - start) / ticksInDay) || 0);
We use this number to calculate some sort of total work estimate. However, now the powers that be demand for this work estimate to no longer include weekends. My first attempt was something like this:
var days = Math.max(0, Math.floor((5 / 7) * (end - start) / ticksInDay) || 0);
Basically, if the range has 7 days, we multiply this by (5 / 7)
to get 5 actual days. 14 would give us 10 actual days. However, this has some flaws.
(5/7)*1 = 0.714285714285714
(5/7)*2 = 1.42857142857143
(5/7)*3 = 2.14285714285714
(5/7)*4 = 2.85714285714286
(5/7)*5 = 3.57142857142857
(5/7)*6 = 4.28571428571429
(5/7)*7 = 5
(5/7)*8 = 5.71428571428571
(5/7)*9 = 6.42857142857143
(5/7)*10 = 7.14285714285714
(5/7)*11 = 7.85714285714286
(5/7)*12 = 8.57142857142857
(5/7)*13 = 9.28571428571429
(5/7)*14 = 10
Rounding (either way) doesn't really work here since ranges like 3 or 4 days, or 10 or 11 days will be wrong. Anything else I've tried amounts to a ugly string or if...thens or weird conditional statements.
The only way I can think of that would be 100% accurate would be to loop through the range, similar to what this answer recommends. However, this code has to run whenever the UI model changes, and needs to calculate work estimates for a potentially large number of rows with potentially large date ranges. I worry an O(n)
solution will be too slow.
Is there a good formula for a date range to exclude 2 out of every 7 whole days?