Jump to content

Archived

This topic is now archived and is closed to further replies.

paul2463

Date Problem

Recommended Posts

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)
[code]

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 ++;
}[/code]

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

Share this post


Link to post
Share on other sites
just replace this line

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

with this

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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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
[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);
      }[/code]

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?

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.