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());
}
6
  • Please include an example that produces your result. Your code isn't correct but it also doesn't work the way you wrote here Commented May 15, 2018 at 14:18
  • nextDay.setDate(start.getDate()+7); should probably be nextDay.setDate(nextDay.getDate()+7); Commented May 15, 2018 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. Commented May 15, 2018 at 14:25
  • Thanks for the information. Commented May 15, 2018 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? Commented May 16, 2018 at 8:31

1 Answer 1

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());
}

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.