How to add months to a date in Office Scripts
22:32 20 Jul 2023

I am trying to create a range of dates in TypeScript. I used the code from here. This successfully adds a single increment for a month, but breaks down upon trying to add to the original value again, as it changes the values of both date and addedDate. How would I go about fixing this? Thanks.

const date = new Date("7 December 2023")
const durVal = 10;
for (let i = 0; i < Number(durVal) - 1; i++) {
  let addedDate = date;
  addedDate = new Date(addedDate.setMonth(addedDate.getMonth()+i));
  console.log(addedDate)
}

The output is: "2023-12-06T13:00:00.000Z", "2023-12-07T13:00:00.000Z", "2023-12-08T13:00:00.000Z"..."2026-12-06T13:00:00.000Z"

I would expect the code to increment the month upwards by 1 each time.

javascript typescript office-scripts