I am attempting to generate an array containing all dates within a specified time frame. To test this, I have chosen Feb 9 - Feb 13 as the date range. Ideally, the resulting array should display [Feb 9, Feb 10, Feb 11, Feb 12, Feb 13]. Upon revisiting an older post, I discovered the method of using New Date() to create new Date objects each time to avoid ending up with a repetitive array like [Feb 13, Feb 13, Feb 13, Feb 13, Feb 13]. However, I am facing an issue where the very first element I push to the array is being overwritten. The output currently shows [Feb 10, Feb 11, Feb 12, Feb 13, Feb 13]. Within my while loop, I've included a print statement to monitor the array after every push, which indicates that the initial element is being replaced. Any insights on why this is occurring and how I can achieve my desired array would be greatly appreciated. Thank you!
Below is the code snippet I am utilizing:
var Current_Date = new Date(Date_start); // Starting from Feb 9
Current_Date = new Date(Current_Date.setHours(Current_Date.getHours() - 24)); // Updating to Feb 8
var Billing_Dates = []
while (Current_Date.valueOf() <= Date_end.valueOf()) {
Current_Date = new Date(Current_Date.setHours(Current_Date.getHours() + 24));
Billing_Dates.push(Current_Date);
Logger.log(Billing_Dates)
}
Simplified Output (showing array status after each push):
[Feb 09]
[Feb 10, Feb 10]
[Feb 10, Feb 11, Feb 11]
[Feb 10, Feb 11, Feb 12, Feb 12]
[Feb 10, Feb 11, Feb 12, Feb 13, Feb 13]
View Image of the Actual Output (including array status after each push)