0

I need to add days to a date in Javascript inside of a loop.

Currently, I have this -

var occurences = 2;
var start_date = "10/2/2020";
for(i=0; i < occurences; i++){
  var repeat_every = 2; //repeat every number of days/weeks/months
  var last = new Date(start_date);
  var day =last.getDate() + repeat_every;
  var month=last.getMonth()+1;
  var year=last.getFullYear();
  var fulldate = month + '/' + day + '/' + year;
  console.log(fulldate);
}

However, this outputs 10/4/2020 twice. I know the issue is because in the 2nd iteration of the loop it again simply adds 2 to the date 10/2/2020, is there a way on the subsequent iterations I can add 2 to the previous result?

Thank you!

2
  • Does this answer your question? Add days to JavaScript Date Commented Oct 2, 2020 at 18:59
  • @zcoop98 unfortunately it does not, I know how to add days to the date, my issue is stemming moreso from the loop itself, once I add the 2 days, I'm struggling to figure out a way to store this result, then on the 2nd iteration of the loop add 2 days to the previous result. Commented Oct 2, 2020 at 19:02

3 Answers 3

1

You can use a multiple of your interval and then use last.setDate( last.getDate() + repeat_every ) to add days and get the correct month and year:

var occurences = 20;
var start_date = "10/2/2020";
for(i=1; i <= occurences; i++){
  var repeat_every = 2*i; //repeat every number of days/weeks/months
  var last = new Date(start_date);
  last.setDate( last.getDate() + repeat_every );
  console.log( `${last.getDate()}/${last.getMonth()+1}/${last.getFullYear()}` );
}

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

1 Comment

Awesome! Ty dude!
0

Make counter i a multiple of repeat_every:

/*<ignore>*/console.config({maximize:true,timeStamps:false,autoScroll:false});/*</ignore>*/
var occurences = 2;
var start_date = "10/2/2020";
for(i=1; i <= occurences; i++){
  var repeat_every = 2*i; //repeat every number of days/weeks/months
  var last = new Date(start_date);
  var day =last.getDate() + repeat_every;
  var month=last.getMonth()+1;
  var year=last.getFullYear();
  var fulldate = month + '/' + day + '/' + year;
  console.log(fulldate);
}
<!-- https://meta.stackoverflow.com/a/375985/ -->    <script src="https://gh-canon.github.io/stack-snippet-console/console.min.js"></script>

3 Comments

This is perfect, exactly what I needed. Thank you!
@CodyCarmichael Alternatively try storing the data outside the loop as a global variable.
@MT0 Ah good catch, this is going to be a more complicated script than I originally thought.
0

I would separate the generation of the days from their formatting / logging. Here we have a function that collects count instances of incrementally adding n days to a date, returning a collection of Date objects:

const everyNDays = (n, count, start = new Date()) => {
  const y = start.getFullYear(), m = start.getMonth(), d = start.getDate()
  return Array.from({length: count}, (_, i) => new Date(y, m, d + (i + 1) * n))
}

const formatDate = (date) =>
  date .toLocaleDateString ()

console .log (
  everyNDays (2, 20) .map (formatDate)
)
// or everyNDays (2, 20) .forEach (date => console .log (formatDate (date)))
.as-console-wrapper {max-height: 100% !important; top: 0}

(If you don't pass a start date, it uses the current date.)

We then map the simple formatDate function over these dates to get an array of strings.

If you would rather start with the current date, you can simply replace new Date(y, m, d + (i + 1) * n) with new Date(y, m, d + i * n).

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.