[2 days left] Whatâ€™s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
Solved

# calculating business days

Posted on 2004-04-15
Medium Priority
1,045 Views
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
Question by:eteran
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• Learn & ask questions
• 8
• 6
• 2
• +1

LVL 3

Expert Comment

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

Author Comment

ID: 10838067
no, It is to calculate contracts. \
0

Author Comment

ID: 10838074
dates
0

LVL 53

Expert Comment

ID: 10838104
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

LVL 3

Expert Comment

ID: 10838142
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

LVL 12

Expert Comment

ID: 10838150

<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

LVL 12

Expert Comment

ID: 10838183
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 Comment

ID: 10838235
what does this exclude weekends
0

LVL 12

Expert Comment

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

Author Comment

ID: 10838332
so I have to define all weekends
or it does it for you
0

LVL 12

Expert Comment

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

Author Comment

ID: 10838339
Sorry got it
0

Author Comment

ID: 10838341
OKay great. Works like a charm
0

Author Comment

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

Author Comment

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

LVL 12

Accepted Solution

lil_puffball earned 2000 total points
ID: 10838445
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

LVL 12

Expert Comment

ID: 10838785
THanks for the points and the A. :)
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your projectâ€™s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQueryâ€¦
###### Suggested Courses
Course of the Month14 days, 13 hours left to enroll

#### 649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.