PDA

View Full Version : Calculating Business Days


pdmisch
06-26-2003, 06:58 PM
I am looking for a way to calculate the number of business days between two dates. On a form I have two input boxes that will capture two dates. When the second date is entered, I am using the onblur command to fire my javascript to calculate the difference between the two dates. Now I need a way to eliminate the weekends from my calculation.


here is my code:


<html>
<head>
<style>
input.hidden
{
border-width:0;
color: red;
background: white;
}
</style>
<script>
function get_year(str, str2)
{
var perf_month=str.substr(0,2);
var perf_day=str.substr(2,2);
var perf_year=str.substr(4,4);
var asgn_month=str2.substr(0,2);
var asgn_day=str2.substr(2,2);
var asgn_year=str2.substr(4,4);
calc_dates(perf_year,perf_month,perf_day,asgn_year,asgn_month,asgn_day)
}
function calc_dates(perf_year,perf_month,perf_day,asgn_year,asgn_month,asgn_day)
{
var asign = new Date(asgn_year, asgn_month, asgn_day);
var perform=new Date(perf_year, perf_month, perf_day);
var one_day=(1000*60*60*24);
var days=(Math.floor((perform.getTime()-asign.getTime())/(one_day)));
contractor_penalty(days)
}
function contractor_penalty(days)
{
if (days <= 3)
{
document.paul.me.value="No contractor Penalty"
}
else if (days > 3 && days <= 10)
{
document.paul.me.value="15% Contractor Penalty"
}
else
{
document.paul.me.value="25% Contractor Penalty"
}
}
</script>
</head>
<body>
<form name ="paul">
<table cellspacing="5">
<tr>
<td align="left"><b> Assgined Date:</b></td>
<td align="right"><input type="text" size="10" name="f_dt_asgn" ></td>
<td>&nbsp;</td>
</tr>
<tr>
<td align="left"><b>Performed Date:</b></td>
<td align="right"><input type="text" size="10" name="perf_date" onblur="get_year(this.value, f_dt_asgn.value)"></td>
<td><input class="hidden" type="text" size="30" name="me" readonly tabindex=100000></td>
</tr>
</table>
</form>
</body>
</html>

eyestrained21
06-27-2003, 04:12 AM
I have tried that one already but in VB language. I suppose the same principle may apply to your problem. I''ll send you soon as you may need it.
:thumbsup: :thumbsup: :p

pdmisch
06-27-2003, 12:50 PM
thanks,
any help would be greatly appreciated.:thumbsup:

RadarBob
06-27-2003, 02:16 PM
I'd take that difference calculation (in days, I assume) and divide by seven. Take the Math.floor of that - i.e. truncate the fraction.

That will be the number of weekends. Mulitply that by 2 and now you have the number of weekend days. Subtract that from the original difference.

In the calculation above you could have "dropped" as many as 6 days. There may be a weekend in there. By figuring out what days are the first & last you dropped you can know if there's one or more weekend days in there. The key to this bit of algorthm, I think, is knowing that Sunday is represented as Zero and Saturday is 6 in Date objects.

Have not accounted for holidays yet. I'd make an array of dates. Each date is a business holiday. Iterate through the array and see if any fall w/in your date range. Subtract 1 for each hit.

My spidey sense is telling me that the complexity of this problem cries out for making an object of this. I've found that thinking OBJECTively makes for overall better implementation.

Volia. soufle.