1

I am trying to loop over and add 7 days to the date, and I am not sure where I am going wrong. The dates get crazy after the first iteration of the loop.
What I am trying to achieve is Jan 1 next day is jan 8 then jan 8 and jan 15 etc. Its incrementing by a month instead of the 8 days. Printing

start day Mon, 01 Jan 2018 00:00:00 GMT
The next day is: Mon, 08 Jan 2018 00:00:00 GMT

start day Mon, 08 Jan 2018 00:00:00 GMT
The next day is:Thu, 08 Feb 2018 00:00:00 GMT

var start = new Date('2018-01-01');
var nextDay = new Date(start);

for (day = 1; day <= 5; day++) 
{
    console.log("start day "+nextDay.toUTCString());
    nextDay.setDate(start.getDate()+7);
    console.log("The next day is:"+nextDay.toUTCString());
}
Josh Mein
  • 28,107
  • 15
  • 76
  • 87
user3525290
  • 1,557
  • 2
  • 20
  • 47
  • Please include an example that produces your result. Your code isn't correct but it also doesn't work the way you wrote here – Luca Kiebel May 15 '18 at 14:18
  • `nextDay.setDate(start.getDate()+7);` should probably be `nextDay.setDate(nextDay.getDate()+7);` –  May 15 '18 at 14:21
  • `getDate()` gets the day of the current month in your local timezone. Your `start` date is actually the last day of the previous year, December 31 (output its value, but not in UTC!). So each iteration, you're setting the date to 38 days, which isn't valid, so the date object increments the month. You can avoid this situation by using `getUTCDate` instead. –  May 15 '18 at 14:25
  • Thanks for the information. – user3525290 May 15 '18 at 16:41
  • Note that *new Date('2018-01-01')* will be parsed as UTC, but you're adding local days. For places that observe daylight saving, this may have unusual effects over daylight saving changeovers. It also means for users west of Greenwich, the date may seem to be one day earlier than expected, see [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) – RobG May 16 '18 at 08:31
  • @Amy—re *Your start date is actually the last day of the previous year*", only for users with offsets less than zero (i.e. west of Greenwich). For those with offsets 0 or greater (i.e. on or east of the prime meridian), it will be the same date (but the time will be equivalent to the local timezone offset). ;-) – RobG May 16 '18 at 08:35

1 Answers1

1

You are currently just always adding 7 days to the start date, what you should do in order to produce the wanted result is:

var start = new Date('2018-01-01');
var nextDay = new Date(start);

for (day = 1; day <= 5; day++) 
{
 console.log("start day "+nextDay.toUTCString());
 nextDay.setDate(start.getDate()+7);
 start.setDate(nextDay.getDate());
 console.log("The next day is:"+nextDay.toUTCString());
}

Also increment the start every time, or else you are just going to always add 7 days to the start, which is always the same date.

I realize this isn't the best way of coding this, you don't need the nextDay variable:

var start = new Date('2018-01-01');

for (day = 1; day <= 5; day++) 
{
 console.log("Start day "+start.toUTCString());
 start.setDate(start.getDate()+7);
 console.log("The next day is:"+start.toUTCString());
}
Luca Kiebel
  • 9,790
  • 7
  • 29
  • 44