Solved

# Count Weekdays Between Two Days

Posted on 2004-09-01
642 Views
I need to place this in a calculated table column of a Brio file (which fully supports Javascript), so I can't use a function. But maybe a Do/While loop would work.

I have a column with an order date, a column with a shipment date and a column that calculates the number of days between the two dates. Now I need a new column that calculates only the number of business days (M-F) between those two dates. For example.

ORDER DATE        SHIPMENT DATE        DELTA IN DAYS          DELTA IN WORK DAYS
6/2/2004              6/14/2004                12                             8
6/25/2004            6/29/2004                4                               2
7/3/2004              7/4/2004                 1                               0
0
Question by:bgarland
• 2
• 2

LVL 24

Accepted Solution

HemanthaKumar earned 125 total points
Here is the code...http://js-x.com/javascript/?view=66

~Hemanth
0

Author Comment

Thanks Hemanth, I found that earlier. It's in the form of a function and I don't think that will work in my application.
0

LVL 14

Assisted Solution

ziffgone earned 125 total points
That is an EXTREMELY difficult question, as you would have to preset a calendar to go off of. In order to figure out the week days, you would have to set up an array to compare with, containing all of the day names of the month.

EX:
var Sept = new Array('Wednesday','Thursday','Friday','Saturday','Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday', ~ repeat all the way on through all days of the month to ,'Thursday');

In the array above, the first day would have to be the day that "01" resides on. For example, the array above is for this month, Sept. 01 is Wednesday, so the array starts with that. It would end with Thursday as Sept 30 falls on a Thurs. To make it simpler, you could abbreviate the names, "Sat" instead of "Saturday". You would have to set up an array for each month you'd want to check. So if you have a 12 month period to check, you would have to set up 12 arrays. I imagine, with a bit of calculating, you could dynamically set up a single array based upon the month number by pushing and delete array values. This way, the correct array would simply be created based upon the month number.

Then you would have to get the days from the Dates:

var arry;
var date = "6/2/2004";
var mon = date.split(/\//)[0];
if(mon == 1){
arry = Jan;
}
if(mon == 2){
arry = Feb;
}
etc... to our example and on to December:
if(mon == 9){
arry = Sept;
}
var orderDate = "6/2/2004";
var orderDay = orderDate.split(/\//)[1];
var shipDate = "6/14/2004";
var shipDay = shipDate.split(/\//)[1];

And now check it against the array we defined by setting the correct Array needed to the "arry" value:

var wrkDays = 0;
var start = orderDay-1;
var end = shipDay-1;
for(i=start;i==end;i++){
if((arry[i] != "Saturday") || (arry[i] != "Sunday")){
wrkDays++;
}
}

The value contained in "wrkDays" should now equal the number of week days within the stated time frame.

I imagine this could also be done with the Julian Calendar and a bunch of math, but that's another story.

Ultimately, I believe you may be hooped as you stated you coudn't use functions, and this would definitely need to be set up in a function.

Regards...
0

Author Comment

I take it back - I can use a function.
0

LVL 14

Expert Comment

Then, actually, Hemanth's post seems like a better, less time consuming way to do it than what I was explaining.

Regards...
0

## Featured Post

### Suggested Solutions

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
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 …
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…