Hi,
I was looking on the web for a function that would calculate the number of week days between 2 dates. I found the following function which I thought was working ok. However, it produces strange results for dates in the last week of October, start of November! I set up a test page to run every date of the year through it (same start and end date, so every test should return '1' for a week day, and '-1' for a weekend). For some reason, it's returning '2' for the days 10/30 through 11/03. Anyone have any ideas!!
<script>
Date.prototype.DaysBetweenBusiness = function(){
//Does not account for holidays! LOL
//return -1 if start or end date is not a business day
if(!this.IsWeekDay() || !arguments[0].IsWeekDay())return -1
var intMilDay = 24 * 60 * 60 * 1000;
var intMilWeek = intMilDay * 7;
var intSDay = this.getDay();
var intEDay = arguments[0].getDay();
this.setDate(this.getDate() + 7 - intSDay);
arguments[0].setDate(arguments[0].getDate() - intEDay);
var intMilDif = arguments[0] - this;
var intDaysTotal = Math.floor(intMilDif/intMilDay);
var intWeeks = Math.floor(intMilDif/intMilWeek);
return xD = (5 - intSDay + intEDay + intDaysTotal -(intWeeks*2)) + 1
}
Date.prototype.IsWeekDay = function(){
return this.getDay() > 0 && this.getDay() < 6
}
function check_dates() {
for(i=1; i<366; i++) {
d1= new Date(document.all['test_date_'+i].value);
d2= new Date(document.all['test_date_'+i].value);
var business_days = d1.DaysBetweenBusiness(d2);
document.all['date_vals'].value = document.all['date_vals'].value + '\n' + business_days + ': ' + document.all['test_date_'+i].value;
}
}
</script>
<form name="form1">
<%
test_date = "01/01/2006"
for i = 1 to 365
%>
<input type="hidden" name="test_date_<%=i%>" value="<%=test_date%>">
<%
test_date = DateAdd("d", 1, test_date)
next
%>
<textarea name="date_vals" rows="20" cols="100"></textarea>
<input type="button" onclick="check_dates()">
</form>
I was looking on the web for a function that would calculate the number of week days between 2 dates. I found the following function which I thought was working ok. However, it produces strange results for dates in the last week of October, start of November! I set up a test page to run every date of the year through it (same start and end date, so every test should return '1' for a week day, and '-1' for a weekend). For some reason, it's returning '2' for the days 10/30 through 11/03. Anyone have any ideas!!
<script>
Date.prototype.DaysBetweenBusiness = function(){
//Does not account for holidays! LOL
//return -1 if start or end date is not a business day
if(!this.IsWeekDay() || !arguments[0].IsWeekDay())return -1
var intMilDay = 24 * 60 * 60 * 1000;
var intMilWeek = intMilDay * 7;
var intSDay = this.getDay();
var intEDay = arguments[0].getDay();
this.setDate(this.getDate() + 7 - intSDay);
arguments[0].setDate(arguments[0].getDate() - intEDay);
var intMilDif = arguments[0] - this;
var intDaysTotal = Math.floor(intMilDif/intMilDay);
var intWeeks = Math.floor(intMilDif/intMilWeek);
return xD = (5 - intSDay + intEDay + intDaysTotal -(intWeeks*2)) + 1
}
Date.prototype.IsWeekDay = function(){
return this.getDay() > 0 && this.getDay() < 6
}
function check_dates() {
for(i=1; i<366; i++) {
d1= new Date(document.all['test_date_'+i].value);
d2= new Date(document.all['test_date_'+i].value);
var business_days = d1.DaysBetweenBusiness(d2);
document.all['date_vals'].value = document.all['date_vals'].value + '\n' + business_days + ': ' + document.all['test_date_'+i].value;
}
}
</script>
<form name="form1">
<%
test_date = "01/01/2006"
for i = 1 to 365
%>
<input type="hidden" name="test_date_<%=i%>" value="<%=test_date%>">
<%
test_date = DateAdd("d", 1, test_date)
next
%>
<textarea name="date_vals" rows="20" cols="100"></textarea>
<input type="button" onclick="check_dates()">
</form>