Click to See Complete Forum and Search --> : calculate number of days from 2 dates given


bobadlin
01-13-2004, 01:06 AM
emmmm...firstly Thanks for viewing this thread...

recently i managed to get a code to calculate number of days from 2 dates given...for e.g (start date -> 01-08-2004) (end date -> 01-13-2004) the result was 6 days !!!...

but the problem wuzz..this number of days include (saturday and sunday)..but my objective is to exclude those days...until now i cant find the solution....that means from example given above i would like the number of days should be 4 days not 6......:confused: :( ...

arghh i really dont know how to exclude those days..can someone help me out????? pleasssee...Thanks....

Pittimann
01-13-2004, 02:34 AM
Hi!

Would you agree that the difference between Jan 8 and Jan 13 is just 5 (and not 6) days and - excluding the weekend - just 3 days? Another examples: Friday, Jan 9th to Sunday, Jan 11th => 0 days (Friday already started and doesn't count, the weekend doesn't count as well); Jan 9th to Jan 12th => 1 (only the "jump" from Friday to Monday counts)...

If so, I could provide some code for you...

Cheers - Pit

bobadlin
01-13-2004, 02:46 AM
Pittimann:

actually its count like this ->

8th Jan to 12th Jan = 3 days(coz i count thursday also)
9th Jan to 12th Jan = 2 days(coz i count friday also)

n everyhting goes like that..since i count the first selected date...

---------------------------------------

n one more thing how do u detect date: for example:

first date = 13 Jan
second date = 13 february

in between those date..can i detect the date (14 Jan, 15 Jan...1 Feb, 2 Feb....13 Feb, 14 feb)...???

Thanks for reply pittmann..:);)

Pittimann
01-13-2004, 02:57 AM
Hi!

Well - ok; for me , that is mathematically incorrect but still logic ;)

I added the startday to the code as well.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled</title>
<script language="JavaScript" type="text/javascript">
<!--
var daysNet=0;
var day1;
//beginn entering start and end date
var begDay=8;
var begMonth=1;
var begYear=2004;
var finDay=13;
var finMonth=1;
var finYear=2004;
//end entering start and end date
begMonth=begMonth-1;//do not edit
finMonth=finMonth-1;//do not edit
startDay = Date.UTC(begYear,begMonth,begDay);
endDay = Date.UTC(finYear,finMonth,finDay);
dayDiff=(endDay-startDay)/86400000+1
day1=startDay;
for (var i = 0; i < dayDiff; i++){
date11= new Date(day1);
if (date11.getDay()!=6&&date11.getDay()!=0){
daysNet++;
}
day1 +=86400000;
}
alert(daysNet);
//-->
</script>
</head>
<body>
</body>
</html>

That code is based on the number of milliseconds passed since the beginning of the year 1970.

If you like to learn more about js date functions etc. please check out this:

http://devedge.netscape.com/library/manuals/2000/javascript/1.5/reference/date.html#1193137

Unfortunately I have to leave and cannot deal with your other questions now. :(

Cheers - Pit