[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 575
  • Last Modified:

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!
0
CVET
Asked:
CVET
  • 7
  • 7
  • 4
  • +2
1 Solution
 
GwynforWebCommented:
This allows you to put the holidays into an array and thay will eb excluded from the count, I do not understand how Us holidays work but I imagune it would not be didfficult to put then in the array even if they are dependent on the year

<script>
function workDays(){
var Holidays =new Array("1/1/0000","7/4/0000/","12/25/0000","12/26/0000")
var holDates= new Array
for (i=1;i<Holidays.length;i++)
  {holDates[i]=new Date(Holidays[i])}
  start=new Date(f1.start.value)
  finish=new Date(f1.finish.value)
  start.setHours(3)
  finish.setHours(3)
 f1.res.value=start
  dayCount=0
  while (start.getTime() <= finish.getTime())
  {
    if (( start.getDay()!=6) && (start.getDay()!=5 ) )
    {holFlag=false
    for (i=1;i<Holidays.length;i++)
      {if ((start.getDay()==holDates[i].getDay()) && (start.getMonth()== holDates[i].getMonth()) ) holFlag=true}
    if (!holFlag)dayCount+=1
    }
     start.setTime(start.getTime()+86400000)
  }
  return (dayCount)
}
</script>

<form name="f1">
  <p>start<input type="text" name="start" size="20"><br>
  finish<input type="text" name="finish" size="20"><br>
  result<input type="text" name="res" size="20"> <input type="button" value="calculate"
  onclick="res.value=workDays()"><br>
  </p>
</form>
0
 
GwynforWebCommented:
remove "f1.res.value=start"  not needed was debug code
0
 
CVETAuthor Commented:
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.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
GwynforWebCommented:
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.

0
 
GwynforWebCommented:
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.
0
 
devicCommented:
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>
0
 
CVETAuthor Commented:
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.
0
 
devicCommented:
>>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?
0
 
CVETAuthor Commented:
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
0
 
devicCommented:
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!

0
 
CVETAuthor Commented:
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.  
0
 
GwynforWebCommented:
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.  
0
 
CVETAuthor Commented:
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.
0
 
devicCommented:
CVET, happy hollidays.
0
 
skqmadCommented:
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.

0
 
CVETAuthor Commented:
I'll be happy to help you out.   What's your email address?
0
 
skqmadCommented:
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.
0
 
CVETAuthor Commented:
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.
0
 
skqmadCommented:
oh sorry...

You can send it on:

skqmad@gawab.com

I am waiting for it anxiously...

Take Care,

Khurram.
0
 
GwynforWebCommented:
CVET, since you seem you be so clever how does your code work for Easter?  ps thanks for the C
0
 
mgcITCommented:
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?
0
 
GwynforWebCommented:
the Easter break in general, eg good Friday & Easter monday, depends on where you are as to which is taken as the holiday.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 7
  • 7
  • 4
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now