Jump to content


Photo

Date Problem


  • Please log in to reply
3 replies to this topic

#1 paul2463

paul2463
  • Members
  • PipPipPip
  • Advanced Member
  • 955 posts
  • LocationUK

Posted 04 July 2006 - 01:34 PM

Hello

I am struggling with something to do with dates and placing them in an array, for information in the script the sy,sm,sd,ey,em,es are all configured and sDate and eDate are correctly configured dates. All I have is a start date and an end date I need to fill all the rest of the dates in as well ( ie sDate = 04 July 2006 and eDate = 07 July 2006, i need to also add the 5th and 6th)

var sDate = new Date(sy,sm,sd);
var eDate = new Date(ey,em,ed);
var dates = new Array();
var count = 0;

while (sDate <= eDate)  {
    dates[count] = sDate;
    sDate.setDate(sDate.getDate()+1);
    count ++;
}

what happens is that I get an array of dates for correct number of days that is should be, but all dates are the same and they are all eDate+1, so for the above example before the code I get an array of length 4 but all slots have 8 July 2006 in it. I am all confused
you cannot affect the past but you can ruin a perfectly good present by worrying about the future

*********************************************************************
The <A HREF="http://www.php.net/d...php">MANUAL</A> is actually a useful resource

#2 nogray

nogray
  • Members
  • PipPipPip
  • Advanced Member
  • 930 posts
  • LocationSan Francisco CA

Posted 05 July 2006 - 04:15 PM

just replace this line

sDate.setDate(sDate.getDate()+1);

with this

sDate.setDate(sDate.getDate()+count);

NoGray.com


#3 paul2463

paul2463
  • Members
  • PipPipPip
  • Advanced Member
  • 955 posts
  • LocationUK

Posted 05 July 2006 - 04:44 PM

Thanks will give that a go

OK tried it but I get exactly the same result, I must admit that I dont understand how the origonal solution didnt work, but it didnt, anyone else got any ideas please
you cannot affect the past but you can ruin a perfectly good present by worrying about the future

*********************************************************************
The <A HREF="http://www.php.net/d...php">MANUAL</A> is actually a useful resource

#4 paul2463

paul2463
  • Members
  • PipPipPip
  • Advanced Member
  • 955 posts
  • LocationUK

Posted 05 July 2006 - 06:12 PM

I have even now changed it for a FOR loop, I have claculated the number of days difference between the start date and end date and use that in the loop, here is the code
var sDate = new Date(sy,sm,sd);
  var eDate = new Date(ey,em,ed);
  var dates = new Array();
  var num = (eDate - sDate)/86400000;
for (i=0 ; i<=num ; i++) {
      dates[i] = sDate;
      sDate.setDate(sDate.getDate()+1);
      }

I still get an array object that is the correct length but it is filled with the same date again, the day after the end date, crazy eh? why isnt it doing what I ask, which is to place sDate into slot i then add a day onto it then place the new sDate into the next slot?
you cannot affect the past but you can ruin a perfectly good present by worrying about the future

*********************************************************************
The <A HREF="http://www.php.net/d...php">MANUAL</A> is actually a useful resource




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users