Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 1049
• Last Modified:

# calculating business days

Here is a complicates question. I need a way to calculate 45 business days from any day of the year. I am going to try to give an example.

Lets say today 5/15/2004 I need to find out what day the 45th business day lands on and it need to exclud holidays and weekend. any Idea where I can find an example like this.
0
eteran
Asked:
• 8
• 6
• 2
• +1
1 Solution

Commented:
Holidays really depends on a company's holiday's calendar... Is this going to be provided as a file or something?
0

Author Commented:
no, It is to calculate contracts. \
0

Author Commented:
dates
0

Commented:
Without a list of the official holidays in the jurisdiction where this is to be applied it is impossible to determine how many business days there are for any given period of time.  Also may we assume that business days are Monday through Friday?

Cd&
0

Commented:
A possible algorithm:
- subtract date2 from date1 (java.util.Date)
- get week number from date1 (GregorianCalendar)
- get week number from date2       "        "
- subtract week number date2 and week number date1 = # of weekends
- multiply # of weekend by 2 (Sat & Sun)
- based on a list of Holidays between the two dates, you can subtract:

number of dates
- number of weekends * 2
- number of holidays
-------------------------------
== number of business days!

Hope this helps!
0

Commented:
How about this? Define your holidays in the array hday.

<script>
//define the holidays
var hday=new Array();
hday[hday.length]="1-1";
hday[hday.length]="12-25";
hday[hday.length]="4-5#2";
hday[hday.length]="4-1#2";

function calcDays(date,n,output){
var newDate=date;
for(i=1;i<=n;i++){
newDate.setDate(date.getDate()+1);
//check if weekend or holiday
if(newDate.getDay()==6||newDate.getDay()==0||isHoliday(newDate)){i--;}
}
output.value=(newDate.getMonth()+1)+"/"+newDate.getDate()+"/"+newDate.getFullYear();
}
function isHoliday(date){
var month,day,num;
for(j=0;j<hday.length;j++){
month=hday[j].split("-")[0];
if(month!=date.getMonth()+1){continue;}
day=hday[j].split("-")[1];
if(day.indexOf("#")==-1){
if(day==date.getDate()){return true;}
}else{
day=day.split("#");num=day[1];day=day[0];
if(day!=date.getDay()){return;}
if(parseInt(date.getDate()/7)+1==num){return true;}
}
}
return false;
}
function setTheDate(date){
date=date.split("/");
return new Date(date[2],date[0]-1,date[1]);
}
</script>

<form>
<br>Date (MM/DD/YY format): <input type=text value="4/15/2004" name=date>
<br># of Business Days: <input type=text value=45 name=numDays>
<br><input type=button value=Calculate onclick="calcDays(setTheDate(this.form.date.value),this.form.numDays.value,this.form.newdate);">
<br>New Date: <input type=text name=newdate readonly>
</form>
0

Commented:
Here is the rules for defining holidays:

There are 2 possible formats:

1) Month - Day
2) Month - Weekday # Number

For example:

1) "1-1" would be January 1 (New Year's Day).

2) "4-5#2" would be the 2nd Friday in April. (Good Friday)

In this example, 4 means April. 5 means Friday. 2 means the 2nd Friday.

If you have any more questions feel free to ask. :)
0

Author Commented:
what does this exclude weekends
0

Commented:
This excludes weekends as well as any holidays you define.
0

Author Commented:
so I have to define all weekends
or it does it for you
0

Commented:
weekends are already incorporated into the code.
holidays must be defined by you.
0

Author Commented:
Sorry got it
0

Author Commented:
OKay great. Works like a charm
0

Author Commented:
Let me ask you something. How do I find the official bank holidays.
0

Author Commented:
What about if I put in the holidays and they land on a weekend will they cont them.
0

Commented:
It doesn't matter.

Here is how the script works (well, a brief description at least):

It loops through for the amount of days specified.
Each time, the day increases by one.
It checks if the day is a weekend. If it is, then skip it.
It checks if the day is a holiday. If it is, then skip it.

So if you put the holiday on the weekend, it'll skip it anyways, since it sees that the day is a weekend.

As for the official bank holidays, it really depends on your region.

Here's a good site, just find your country:

http://www.national-holidays.com/

For united states, this is the page:

http://www.jours-feries.com/index_pays.php3?id_pays=60&id_langue=2
0

Commented:
THanks for the points and the A. :)
0

## Featured Post

• 8
• 6
• 2
• +1
Tackle projects and never again get stuck behind a technical roadblock.