javascript Calculate Business Days Help

lucaPiper

Recruit
My boss sits beside me all day long, and I cannot figure out how to do a "simple" task he has given me.

He wants me to implement a process that will take the submit date of a form, add three business days to that, and thus derive an end date. (Business days are defined as M - F, not including national US holidays).

I am illiterate at javascript, but have found this function on the internet that purports to do what I need it to do:
function businessDays(n,D){
D= D || new Date();
var num=Math.abs(n);
var tem,count=0;
var dir= (n<0)? -1: 1;
while(count< num){
D= new Date(D.setDate(D.getDate()+dir));
if(D.isHoliday())continue;
tem=D.getDay();
if(tem!=0 && tem!=6) ++count;
}
return D;
}
Date.prototype.isHoliday=function(){
var A=[this.getMonth()+1,this.getDate()]
var hol={
'Christmas':[12,25],
'GroundHogDay':[2,2]
}
var tem;
for(var p in hol){
tem= hol[p];
if(A[0]==tem[0] && A[1]==tem[1]) return p;
}
return false;
}

My questions:
1. Do the functions go inside script tags in the html header (I assume so)?
2. Do I call the functions from my html form as <input type="button" value="submit" onclick="businessDays(n,D);">
3. How do I get the value into the parenthetical statement businessDays(n,D)
4. I am already inputting my submit value as a timedate stamp in a mySQL database. What code will write the final end date to a php variable I can pass to my database.
5. Is there a way to get javascript to display the results of the function onscreen while I am testing it, just to see if it is working.

I know I am asking a lot, but I am in a position where I can see the individual dots, I just don't know how to connect them. Please help!
 
best link for you JavaScript Tutorial

1. Yes. Put them before the <head> start tag.

Put you functions between these tags

<script type="text/javascript">

......

......

</script>

2. yes. But instead of (n,D) the values of (no of days, date from form field) should be passed to the function there i think.

ex: <input type="button" value="submit" onclick="businessDays(3, 12/12/2009);">

3. see above - it will automatically pass the variables to your function.

4. dunno.

5. you can use alert to get the output in a browser popup.

example here

don't know why no one answered this yet, but you should double check my answers too. I might be wrong. Long time since I touched javascript.
 
Back
Top