Solved

calculating business days

Posted on 2004-04-15
17
1,037 Views
Last Modified: 2008-03-17
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
Comment
Question by:eteran
  • 8
  • 6
  • 2
  • +1
17 Comments
 
LVL 3

Expert Comment

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

Author Comment

by:eteran
Comment Utility
no, It is to calculate contracts. \
0
 

Author Comment

by:eteran
Comment Utility
dates
0
 
LVL 53

Expert Comment

by:COBOLdinosaur
Comment Utility
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

by:int_20h
Comment Utility
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

by:lil_puffball
Comment Utility
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
 
LVL 12

Expert Comment

by:lil_puffball
Comment Utility
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

by:eteran
Comment Utility
what does this exclude weekends
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 12

Expert Comment

by:lil_puffball
Comment Utility
This excludes weekends as well as any holidays you define.
0
 

Author Comment

by:eteran
Comment Utility
so I have to define all weekends
or it does it for you
0
 
LVL 12

Expert Comment

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

Author Comment

by:eteran
Comment Utility
Sorry got it
0
 

Author Comment

by:eteran
Comment Utility
OKay great. Works like a charm
0
 

Author Comment

by:eteran
Comment Utility
Let me ask you something. How do I find the official bank holidays.
0
 

Author Comment

by:eteran
Comment Utility
What about if I put in the holidays and they land on a weekend will they cont them.
0
 
LVL 12

Accepted Solution

by:
lil_puffball earned 500 total points
Comment Utility
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

by:lil_puffball
Comment Utility
THanks for the points and the A. :)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
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…

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

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

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now