Count Weekdays Between Two Days

Posted on 2004-09-01
Last Modified: 2012-06-21
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.

6/2/2004              6/14/2004                12                             8
6/25/2004            6/29/2004                4                               2
7/3/2004              7/4/2004                 1                               0
Question by:bgarland
  • 2
  • 2
LVL 24

Accepted Solution

HemanthaKumar earned 125 total points
ID: 11955656
Here is the code...


Author Comment

ID: 11955697
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.
LVL 14

Assisted Solution

ziffgone earned 125 total points
ID: 11956171
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.

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;
      if((arry[i] != "Saturday") || (arry[i] != "Sunday")){

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.


Author Comment

ID: 11956806
I take it back - I can use a function.
LVL 14

Expert Comment

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


Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
c# LinkButton OnClientClick 2 33
initialise the object angular2 4 30
window.setInterval not firing 7 29
regx  exclude  pattern 6 26
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. …
Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
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…

939 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

10 Experts available now in Live!

Get 1:1 Help Now