View Full Version : Date Looping.... help

02-03-2004, 05:40 PM
Im trying to come up with a 'clean' loop that accepts two dates in mm/dd/yyyy format then performs a loop to step thru each day inclusive. Ive written one but it is 'kludgie' i.e not elegant

02-03-2004, 06:49 PM
My suggestion, not a stroke of brilliance but should work, note the whole thing is in a function that is not really necesary but I prefer putting things in functions, the loop it self is in the bottom of the function.

var startdate='22/03/2003';
var enddate='30/03/2003';

function loopdates(aFrom,aTo)
function splitDate(aDate)
return aDate.split('/');
var DateLength=new Date(0,0,2)-new Date(0,0,1);
aFrom=new Date(aFrom[2],aFrom[1],aFrom[0])-new Date(0,0,0);
aTo=new Date(aTo[2],aTo[1],aTo[0])-new Date(0,0,0);


Note the loop is inclusive of both dates

02-03-2004, 06:58 PM
Thanks..... that should do itg

02-04-2004, 05:54 PM
Thanks for the outline. I should comment that the script requires a few 'minor' mods.

1) Must remember to subtract 1 from month when calling the Date function.

2) If you want to actually be able to 'see' each date as you go thru the loop a bit more coding is required. This is my final working loop :

aFrom=splitDate(frm.fromdate.value); // mm/dd/yyyy
var DateLength=new Date(0,0,2)-new Date(0,0,1); // one day
var aFromdate=new Date(aFrom[2],aFrom[0]-1,aFrom[1])-new Date(0,0,0);
var stopdate =new Date(aTo[2],aTo[0]-1,aTo[1])-new Date(0,0,0);
var effdate = "";
var xafromDate = new Date();
var dcount = 0;
while(aFromdate <= stopdate)

xafromDate = new Date( aFrom[2],aFrom[0]-1,(parseInt(aFrom[1]) + dcount++));
aFromdate = aFromdate+DateLength; //inc date
strin = strlist.length + 1;
strindex = strin + " ";
strlist[strlist.length] = bpadit(strindex,3) + " " + bpadit(xcurChoice,6) + lpadit(curAmount,8) + " " +
boxstr + " " + effdate;

frm.selChoices[frm.selChoices.length] = new Option(strlist[strlist.length-1],strlist[strlist.length-1]);

} //while < stopdate


02-04-2004, 06:25 PM
K sounds rigth lol :), btw I would suggest u always put a while loops incrementer in the start or end of the loop, unless u got a reason to put i any other way, that way u always know where to look for what increments the loop.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum