Link to home
Start Free TrialLog in
Avatar of CVET
CVETFlag for United States of America

asked on

Function to calculate work days between 2 dates

The function should take a start date and an end date in the format DD/MM/YYYY as parameters.  It has to return the number of work days between those two dates, keeping in mind that weekends are not work days.  The following are also not work days:  1/1, 7/4, Thanksgiving and the day after Thanksgiving, and Christmas.  The holidays that are not fixed dates makes this especially difficult for me.   I would be most grateful if someone could provide me with such a function.   Thanks!
ASKER CERTIFIED SOLUTION
Avatar of GwynforWeb
GwynforWeb
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
remove "f1.res.value=start"  not needed was debug code
Avatar of CVET

ASKER

Thanks, but I cannot simply use an array.   This function should work regardless of the year.  And Thanksgiving is not a fixed holiday.  It is always the last Thursday and Friday of November.  That complicates things a bit.
The array will hold the fixed holidays, there is no other reasonable way of doing it and the code I have written works regardless of the year for those, (the 0000 year is a dummy value to make the code work). To do the Thursday and Friday of November I assume it is the last Friday the unless the last day of the month is Thursday, and I can code that. As to how you will do Easter I have no idea you had better ask the Vatican as they announce it a few years ahead of time. For the thanksgiving I can put it in the last element of the array and vary it as the loop goes through depending on the years. (you can reserve other array elements for any other variable holidays). If you do not want an array at all then best of luck you will need a HUGE if/select statement and I am not interested in coding that.

Basically I can make it work for thanksgiving for any pair of dates in from any pair of years. If that is what you want then I can code that. I have a feeling you want something that runs in to hundreds of lines of code in which case I suggest looking on the web.

I should point out the code I wrote works regardless of the year of the start date and the year of the finish date. Try it.
Avatar of devic
here is my example:
=====================================
<script>
function GetWorkDays(startDate,endDate)
{
      var d1=startDate.split("/")[0]*1;
      var m1=startDate.split("/")[1]*1-1;
      var y1=startDate.split("/")[2]*1;
      var d2=endDate.split("/")[0]*1;
      var m2=endDate.split("/")[1]*1-1;
      var y2=endDate.split("/")[2]*1;
      
      var dt1 = new Date(y1,m1,d1);
      var dt2 = new Date(y2,m2,d2);
      var days = (dt2-dt1)/1000/60/60/24;
      var workdays=days+1;
      for(var i=0;i<days+1;i++)
      {
            if(isHoliday(dt1,i))
            {
                  workdays--
            }
      }
      return workdays;
}
// FORMAT DD/MM/[YYYY]
// Year is optional, if not declared, then is every Year.
var Holidays =
[
"1/1/",
"25/12/",
"26/12/",
"27/12/",
"24/7/",
"15/1/2004"
]
function isHoliday(objDate,intDays)
{
      var newDate = objDate.valueOf()+(intDays*86400000);
      var curDate = new Date(newDate);
      var strCurDate = curDate.getDate()+"/"+(curDate.getMonth()+1)+"/"+curDate.getFullYear()
      for(var i=0;i<Holidays.length;i++)
      {
            if(strCurDate.indexOf(Holidays[i])!=-1)
            {
                  return true;
            }
      }
      
      if(curDate.getDay()==6 ||curDate.getDay()==0) return true;
      return false;
}
</script>
<button onclick=alert(GetWorkDays("15/12/2003","27/12/2003"))>GetWorkDays("15/12/2003","27/12/2003")</button><br>
<button onclick=alert(GetWorkDays("15/12/2003","21/12/2003"))>GetWorkDays("15/12/2003","21/12/2003")</button><br>
<button onclick=alert(GetWorkDays("27/12/2003","27/12/2004"))>GetWorkDays("27/12/2003","27/12/2004")</button><br>
Avatar of CVET

ASKER

I finished my script on my own.  Thanks for trying to help, but I don't even know how (or whether) to award points in this case.  I specifically asked for help with floating holidays and said that using arrays would not work for me.  I don't know why you would waste your time trying to help and ignore the main point of the problem.

Anyway, I'm not a frequent user of experts exchange, so I don't know how these situations are resolved.  I'd appreciate suggestions on how to award points for answers that are not helpful.
>>I specifically asked for help with floating holidays and said that using arrays would not work for me

what problem do you have with arrays?

>>I finished my script on my own

without arrays?
Avatar of CVET

ASKER

The problem with arrays is that they do NOT work for floating holidays.  They're fine for fixed holidays, but that wasn't my main problem.  How does an array help you calculate the 4th Thursday of November for any year?  I found an algorithm on the web that does this and it is no more than 15 lines of code.  Here's the website if you're curious:  http://www.michaelthompson.org/technikos/holidays.php
ok, ok, you can manual write for 5 years or do you need this for 100 years?

and in one year you will modify this anyway or forget this script :)

anyway good look!

Avatar of CVET

ASKER

What the heck are you talking about?  I'm beginning to think I have more experience than the people trying to help me.  Did you look at the algorithm described in the link I posted?  It is completely automated, no modifications or hard coding required.  The script I have written myself will work forever.   True, this script is probably not going to be used 100 years from now.  What does that have to do with anything?  I take pride in my work and I don't like to write code with severe limitations when with a little more research I can produce completely automated code.  
CVET,
  Knowing when the floating holidays are in a year and calculating the number of work days  between 2 dates are  two different things, the second requires a knowledge of the first and it is best put into an array togehter with the fixed holidays.

  Your question did not specify just the floating holidays but was to calculale the number of work days which requires the fixed hoidays as well. If you feel you have more experience then the people on this site then best of luck to you. Reading you response I do not think you do.  
Avatar of CVET

ASKER

GwynforWeb,

Sorry it took so long - I just came back from vacation.  That was part of the reason why it was so important to get the answer to my problem right away.  I feel bad that this topic has degenerated into a shouting match.   I did write my own script.  I could have used arrays for the fixed holidays, but I chose not to.  The algorithm for floating holidays that I posted a link to earlier is actually quite nice, I don't know why devinc was so cynical about it. The script evolved into a lot more than what I originally asked for here.  Requirements creep, you know how that goes.  I ended up having to account for fixed holidays falling on the weekend and being shifted to the following Monday or the previous Friday.  If you're interested in seeing the finished script, comment here with your email address and I'll send it to you.
CVET, happy hollidays.
Avatar of skqmad
skqmad

Hi CVET,

Hope you must be back from vacations now. How went the holidayz?

I came here in search of the same script you were in need of. I searched a lot but almost all the scripts ended using the arrays, and my logic is same to you regarding arrays. Can u plz let me use the script you wrote. I will be really thankful to you.

Take Care,

Khurram.

Avatar of CVET

ASKER

I'll be happy to help you out.   What's your email address?
Hi CVET,

How r u? Hope that you will find this email in the best of ur health.

Nice to see a quick response from you. My email address is skqmad@hotmail.com.

Will wait for your email.

Take Care

Khurram.
Avatar of CVET

ASKER

My email to you was returned with the following message.  You seem to have gone over your storage quota.  Do you have another email address?

Final-Recipient: rfc822;skqmad@hotmail.com
Action: failed
Status: 5.2.3
Diagnostic-Code: smtp;552 5.2.3 This message is larger than the current
system limit or the recipient's mailbox is full. Create a shorter
message body or remove attachments and try sending it again.
oh sorry...

You can send it on:

skqmad@gawab.com

I am waiting for it anxiously...

Take Care,

Khurram.
CVET, since you seem you be so clever how does your code work for Easter?  ps thanks for the C
CVET, I'm working on a similar project and hoping you still have the code...please let us know how you did this.

Thanks

GwynforWeb:
Isn't Easter on a Sunday? and if so that should be taken care of by excluding weekends?
the Easter break in general, eg good Friday & Easter monday, depends on where you are as to which is taken as the holiday.