Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Custom datediff function giving weird results for Oct / Nov!

Status
Not open for further replies.

cawthor

Programmer
May 31, 2001
89
US
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>
 
ounds like that might be when the clocks change by an hour. Try using UTC date functions to get around this... or setting the hour to something like 6am.

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
This is a puzzler, but here is a clue that may help...

This is the only time of year when a certain internal value "intMilDif" becomes wierd.

The formula to calculate is as follows:

var intMilDif = arguments[0] - this;

At the "wrong" time of year it value is:

intMilDif=[-608400000],

It is consistent for the rest of the year:

intMilDif=[-604800000],

LOOK CAREFULLY (84 versus 48)
THEY ARE NOT THE SAME VALUE!

The diffrerence must be somewhere in the values for:

var intMilDif = arguments[0] - this;



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

BTW


My trace shows...

Tue Oct 24 00:00:00 EDT 2006 intSDay=[3], intEDay=[3], intDaysTotal=[-7], intWeeks=[-1], intMilDay=[86400000], intMilWeek=[604800000], intMilDif=[-604800000],
Wed Oct 25 00:00:00 EDT 2006 intSDay=[4], intEDay=[4], intDaysTotal=[-7], intWeeks=[-1], intMilDay=[86400000], intMilWeek=[604800000], intMilDif=[-604800000],
Thu Oct 26 00:00:00 EDT 2006 intSDay=[5], intEDay=[5], intDaysTotal=[-7], intWeeks=[-1], intMilDay=[86400000], intMilWeek=[604800000], intMilDif=[-604800000],
Fri Oct 27 00:00:00 EDT 2006
Sat Oct 28 00:00:00 EDT 2006
Sun Oct 29 00:00:00 EDT 2006 intSDay=[1], intEDay=[1], intDaysTotal=[-8], intWeeks=[-2], intMilDay=[86400000], intMilWeek=[604800000], intMilDif=[-608400000],
Mon Oct 30 00:00:00 EST 2006 intSDay=[2], intEDay=[2], intDaysTotal=[-8], intWeeks=[-2], intMilDay=[86400000], intMilWeek=[604800000], intMilDif=[-608400000],
Tue Oct 31 00:00:00 EST 2006 intSDay=[3], intEDay=[3], intDaysTotal=[-8], intWeeks=[-2], intMilDay=[86400000], intMilWeek=[604800000], intMilDif=[-608400000],
Wed Nov 1 00:00:00 EST 2006 intSDay=[4], intEDay=[4], intDaysTotal=[-8], intWeeks=[-2], intMilDay=[86400000], intMilWeek=[604800000], intMilDif=[-608400000],
Thu Nov 2 00:00:00 EST 2006 intSDay=[5], intEDay=[5], intDaysTotal=[-8], intWeeks=[-2], intMilDay=[86400000], intMilWeek=[604800000], intMilDif=[-608400000],
Fri Nov 3 00:00:00 EST 2006
Sat Nov 4 00:00:00 EST 2006
Sun Nov 5 00:00:00 EST 2006 intSDay=[1], intEDay=[1], intDaysTotal=[-7], intWeeks=[-1], intMilDay=[86400000], intMilWeek=[604800000], intMilDif=[-604800000],
Mon Nov 6 00:00:00 EST 2006 intSDay=[2], intEDay=[2], intDaysTotal=[-7], intWeeks=[-1], intMilDay=[86400000], intMilWeek=[604800000], intMilDif=[-604800000],












 
Thanks for the help guys. It looks like it was the clock change that was affecting it. I'm still not sure why it would return '2' for that one week though, wouldn't it be the rest of the year if that was the issue? If I add the following code it works perfectly:

d1.setHours('5');
d2.setHours('6');

Now it is comparing the first date at 5am with the second date at 6am. It works, but I'm not really sure why! I would think this should return '2' for every day up until Oct when the clocks change (as it should calculate 25 hours before change, and 24 after change)?? As it is, it returns '1' for every weekday, and '-1' for every weekend. If anyone can explain this to me then please do!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top