Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Dec 2003
    Location
    Bahamas
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Date Looping.... help

    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

  • #2
    Regular Coder
    Join Date
    Jul 2002
    Posts
    698
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

    Code:
     
    <script>
    var startdate='22/03/2003';
    var enddate='30/03/2003';
    loopdates(startdate,enddate);
    
    function loopdates(aFrom,aTo)
    {
      function splitDate(aDate)
    	{
    	  return aDate.split('/');
    	}
      aFrom=splitDate(aFrom);
    	aTo=splitDate(aTo);
    	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);
    	while(aFrom<=aTo)
    	{
    
    	aFrom=aFrom+DateLength;
    	}
    }
    </script>
    Note the loop is inclusive of both dates

  • #3
    New Coder
    Join Date
    Dec 2003
    Location
    Bahamas
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks..... that should do itg

  • #4
    New Coder
    Join Date
    Dec 2003
    Location
    Bahamas
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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
    aTo=splitDate(frm.todate.value);
    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++));
    effdate=date2str(xafromDate);
    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

    Thanks

  • #5
    Regular Coder
    Join Date
    Jul 2002
    Posts
    698
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •