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 Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date Checking ~ between dates

Status
Not open for further replies.

Suthern

Technical User
Joined
Dec 5, 2000
Messages
122
Location
US
I'm building a webpage interface for an RV park database system.

Currently I'm working on a webpage that will allow a 'agent' to see what sites are availible. Using ASP and javascript.

I've having trouble figuring out the currect scripting for only returning the site numbers that will be availible for a certain time slot.

I have a function (Code below), which querys the database and returns all the important data from table 'Visit'. The function then trys to select just the correct data out of the data from the database, and return it in a usable form.

For example: Customer A is staying from Jan 4th to Jan 9th.
Customer B's schedule has the possiblity of 4 positions.
[ol][li]Before A's scheduled time(ex.1st-3rd),[/li]
[li]Overlapping the start of A's scheduled time(ex.2nd-5th),[/li]
[li]Overlapping the end of A's Scheduled time(ex.8th-12th),[/li]
[li]After A's scheduled time(ex.12th-15th)[/li][/ol]

Obviously, we don't want this function returning any sites that match 2 or 3. Below are the functions.
======================CODE=======================
Code:
function printArrayOptions(name,options) {
   Response.write(&quot;<select width='3' name=&quot;+name+&quot;>&quot;);
   for (i=1;i<options.length;i++) {
      if (options[i]==true) {
        Response.write(&quot;   <option value=\&quot;&quot;+i+&quot;\&quot;>&quot;);
        Response.write(i);
        Response.write(&quot; </option>\n&quot;);
      }
   }
   Response.write(&quot;</select>&quot;);
} 



function GetSitesByDate(StartDate,EndDate)
{
	var conn = makeConnection;
	var sites = new Array();
	var selSQL = &quot;Select SiteID,StartDate,EndDate from Visit&quot;; 
	var ss = executeSQL(selSQL);
	
	var ri=0, i=0;
	var ss2 = new Object();
	while(!ss.eof){			//Per line in results from visit table
		ss2.SiteID = new String(ss(&quot;SiteID&quot;));
		ss2.StartDate = new String(ss(&quot;StartDate&quot;));
		ss2.EndDate = new String(ss(&quot;EndDate&quot;));
		for (test=StartDate;test<=EndDate;test.setDate(test.getDate+1)) {		//Testing each Day the custoemr is staying
			//sites[test+100] = true;
			sites[ri+75] = true;  //debugging
			if (test>ss2.StartDate&&test<ss2.EndDate){ 
				sites[ss(0)] = false;
			} else { 
				sites[ss(0)] = true;
			}
			sites[ri+30] = true; //debugging
			ri++;
		}
		ss.movenext();
		sites[ss2.SiteID + 100] = true; //debugging
		i++;
	};
	
	var max=40;
	sites[48] = true;

	//sites = [0,1,1,0,1,0,1,0,1,1,1,1,0,1,1,1,0,0,0,1,0,1]; //testing array
	ss.close();
	return sites;
}
=====Code for printing=======
<% var sites = GetSitesByDate(startDate,endDate);
			printArrayOptions(&quot;SiteID&quot;,sites); %>
=========END of code=========

The startDate and endDate fields are 'date/time' fields.
I can get data from the database fine, so thats not a problem. Also, the values that are passed into 'GetSitesBydate' are Date() objects.The SQL works fine.

Basically, the code is not performing as desired. It dosn't loop enough times somewhere (Yea, I wrote the code :P), and I'm having trouble finding whats wrong.

If anyone has any suggustions/hints/tips, By all means, let me know!

Thanx in advance.
-Suthern
 
for (test=StartDate;test<=EndDate;test.setDate(test.getDate+1))

test.getDate()+1

;-)
-pete

 
here is better code


Code:
function printArrayOptions(name,options) {
   Response.write(&quot;<select width='3' name=&quot;+name+&quot;>&quot;);
   for (i=1;i<options.length;i++) {
      if (options[i]==true) {
        Response.write(&quot;   <option value=\&quot;&quot;+i+&quot;\&quot;>&quot;);
        Response.write(i);
        Response.write(&quot; </option>\n&quot;);
      }
   }
   Response.write(&quot;</select>&quot;);
} 

function add_day(adate) {
    return new Date(adate.getTime() + 86400000);
}

function GetSitesByDate(StartDate,EndDate)
{
	var conn = makeConnection;
	var sites = new Array();
	var selSQL = &quot;Select SiteID,StartDate,EndDate from Visit&quot;; 
	var ss = executeSQL(selSQL);
	
	var ri=0, i=0;
	var ss2 = new Object();
	while(!ss.eof){			//Per line in results from visit table
		ss2.SiteID = new String(ss(&quot;SiteID&quot;));
		ss2.StartDate = new Date(ss(&quot;StartDate&quot;));
		ss2.EndDate = new Date(ss(&quot;EndDate&quot;));
		for (test=new Date(StartDate);test.getDate()<=EndDate.getDate()||ri<300;add_day(test)) {		//Testing each Day the custoemr is staying
			//sites[test+100] = true;
			//sites[ri+75] = true;  //debugging
			if (test.getDate()>ss2.StartDate.getDate()&&test.getDate()<ss2.EndDate.getDate()){ 
				sites[ss(0)] = false;
			} else { 
				sites[ss(0)] = true;
			}
			sites[ri+i+30] = true; //debugging
			ri++;
		}
		ss.movenext();
		sites[ss2.SiteID + 100] = true; //debugging
		i++;
	};
	
	var max=40;
	//sites[48] = true;

	//sites = [0,1,1,0,1,0,1,0,1,1,1,1,0,1,1,1,0,0,0,1,0,1]; //testing array
	ss.close();
	return sites;
}
 
Why can't we edit messages here? Anyway, The code above still has problems...

Any more good clues?
-Suthern

P.S. TY for your help Palbano!
 
>> The code above still has problems...

You need to provide a better description of the problems eh? My crystal ball is out for repair just now ;-)

-pete


 
LOL. Sorry.

The last pasted code above causes the server to use 100% cpu for about 2 min, deliver the result to the browser, and continue using 100% cpu for another min. The code that is below (In this post) does not cause the CPU usage to go up, but also does not give me the correct result :(

One thing that I assumed (correct me if I'm wrong), is that I can pull a date() object straight out of a date field in a database, and the values will be the same format as a date() object. I hope that is correct!

Problem: the for loop keeps going and going and going (Thats why I put a 300 max on it). I can't figure out why the for loops keeps rolling.. add_day seems to work correctly, and its only adding 1 day at a time. the for loop never exceeds &quot;test<=EndDate&quot;, so it never stops. Why it dosn't stop being true, I have no clue. test starts out as the startdate value, then with each cycle gets one more day, then it should end when it reaches endDate. It doesn't end though!

Code:
function printArrayOptions(name,options) {
   Response.write(&quot;<select width='3' name=&quot;+name+&quot;>&quot;);
   for (i=1;i<options.length;i++) {
      if (options[i]==true) {
        Response.write(&quot;   <option value=\&quot;&quot;+i+&quot;\&quot;>&quot;);
        Response.write(i);
        Response.write(&quot; </option>\n&quot;);
      }
   }
   Response.write(&quot;</select>&quot;);
} 

function add_day(adate) {
    return new Date(adate.getTime() + 86400000);
}

function GetSitesByDate(StartDate,EndDate)
{
	var conn = makeConnection();
	var sites = new Array();
	var selSQL = &quot;Select SiteID,StartDate,EndDate from Visit&quot;; 
	var ss = executeSQL(selSQL);
	
	var ri=0, i=0;
	var ss2 = new Object();
	while(!ss.eof){			//Per line in results from visit table
		ss2.SiteID = new String(ss(&quot;SiteID&quot;));
		ss2.StartDate = new Date(ss(&quot;StartDate&quot;));
		ss2.EndDate = new Date(ss(&quot;EndDate&quot;));
		for (test=new Date(StartDate);test<=EndDate && ri<300;add_day(test)) {		//Testing each Day the custoemr is staying
			//sites[test+100] = true;
			//sites[ri+75] = true;  //debugging
			if (test.getDate()>ss2.StartDate.getDate()&&test.getDate()<ss2.EndDate.getDate()){ 
				sites[ss(0)] = false;
			} else { 
				sites[ss(0)] = true;
			}
			sites[ri+i+30] = true; //debugging
			ri++;
		}
		ss.movenext();
		sites[ss2.SiteID + 100] = true; //debugging
		i++;
	};
	
	var max=40;
	//sites[48] = true;

	//sites = [0,1,1,0,1,0,1,0,1,1,1,1,0,1,1,1,0,0,0,1,0,1]; //testing array
	ss.close();
	conn.Close();
	return sites;
}
 
>> One thing that I assumed

BAD technique. In this case you should be correct by why assume when you can spend 2 minutes writing and executing code to prove/disprove the assumption.

For example you could take out the for loop entirely and send the date variable string representations to the browser so you can see them. Then if everything is correct you can add a single loop and continue to write the date variable values to the browser so you can see them.

The only reason for the behavior is flawed logic or an unexpected result from an operation. The only way to discover which and where is to watch the values as they change.

-pete


 
You are right, that is Bad technique. Thanks for reminding me! I checked it out (wrote lines of code to test) and they are indeed the same format :) Now I just have to go find the flawd logic. ty for the help!
-Suthern
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top