My code example was wrong. Day numbers start with 0 (Sunday). So omitting Saturday and Sunday has to be done by [5, 6] as nonWorkingDays array. If I test with that, everything looks fine. Now we have to agree if the last day (end) should also be included or not…
patrick:
My code example was wrong. Day numbers start with 0 (Sunday). So omitting Saturday and Sunday has to be done by [5, 6] as nonWorkingDays array. If I test with that, everything looks fine. Now we have to agree if the last day (end) should also be included or not…
We probably don’t have the same plugin version because I still get the same results. I use the last version you sent me on Saturday 17h17 (GMT)
Here is my code:
var days = plugins.DateUtils.getWorkingDays(new Date(arguments[0]), new Date(arguments[1]), [5,6]);
return (days.length);
EDIT: in fact I just noticed that it’s not a problem with Sunday which is not excluded. Actually no day is excluded and the end date is not counted that’s all.
First, I was confusing month and dayOfWeek in Java. Day numbers start with 1 = Sunday and end with 7 = Saturday. Second, there is a bug in the build you are using. I will send you an update.
Here’s another pure Javascript solution. This one requires my mod_datejs module (http://code.google.com/p/servoymoddatejs/downloads/list), but could also easily be done without it, it just improves the readability a little…
function workDays()
{
var st = arguments[0];
var en = arguments[1];
var days = Math.round((en.getTime()-st.getTime())/(1000*60*60*24));
var base = Math.round(days / 7) * 5;
var mod = days % 7;
var day = en.clone().addDays(-mod).getDay();
for(var ix=0; ix<=mod; ix++)
{
var d = en.clone().addDays(-ix);
if(d.getDay() != 0 && d.getDay() != 6) base++;
}
return base;
}
It’s a little more mathematical than a pure loop test, so should perform better for a large date range. Not extensively tested, but I think I got it right.
greg.
patrick:
First, I was confusing month and dayOfWeek in Java. Day numbers start with 1 = Sunday and end with 7 = Saturday. Second, there is a bug in the build you are using. I will send you an update.
It looks ok now!
Thanks for your help.
agiletortoise:
Here’s another pure Javascript solution. This one requires my mod_datejs module (http://code.google.com/p/servoymoddatejs/downloads/list), but could also easily be done without it, it just improves the readability a little…function workDays()
{
var st = arguments[0];
var en = arguments[1];
var days = Math.round((en.getTime()-st.getTime())/(10006060*24));
var base = Math.round(days / 7) * 5;
var mod = days % 7;
var day = en.clone().addDays(-mod).getDay();
for(var ix=0; ix<=mod; ix++)
{
var d = en.clone().addDays(-ix);
if(d.getDay() != 0 && d.getDay() != 6) base++;
}
return base;
}
It's a little more mathematical than a pure loop test, so should perform better for a large date range. Not extensively tested, but I think I got it right. greg.
Cool.
Thanks for your contribution
When I have done this in the past (in other environments) I found it best to simply create a table of working days that would become the company (or school, or whatever) calendar. Then provide a UI for the user to add** additional non-work days such as religious or national holidays (or, in the case of education solutions, Spring Break, etc.). It’s a tiny little narrow table, easy to script generating a whole year of non-weekend days, and makes it really easy to script things like “7 work days after today”.
**EDIT: i mean remove other non-work days, doh
kazar