Reputation: 15
I am trying to create an array of all dates within a given time frame. For testing purposes, I am using Feb 9 - Feb 13, so the desired array should be [Feb 9, Feb 10, Feb 11, Feb 12, Feb 13]. Thanks to an old post, I learned to use New Date() to create new Date object every time, so that the array doesn't end up like [Feb 13, Feb 13, Feb 13, Feb 13, Feb 13]. But, it is still overwriting the very first element that I push to the array; It is outputting [Feb 10, Feb 11, Feb 12, Feb 13, Feb 13]. I have included a print statement inside my while loop, to track the array after every push, which is how I know the first element is the one getting overwritten. I was hoping for any insight into why this is happening, and how to get my desired array? Any help is much appreciated, thank you!
Here is the code I am using:
var Current_Date = new Date(Date_start) // It starts at Feb 9
Current_Date = new Date(Current_Date.setHours(Current_Date.getHours() - 24)); // 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 (including the status of the array after every 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]
Image of the Exact Output (including the status of the array after every push)
Upvotes: 1
Views: 63
Reputation: 64032
function makeDateArray(start = '2/9/2021',end = '2/13/2021') {
let da = [];
let sd = new Date(start);
let ed = new Date(end);
let n = 0;
do {
da.push(new Date(sd.getFullYear(), sd.getMonth(), sd.getDate() + n++));
} while (new Date(da[da.length - 1]).valueOf() < ed.valueOf());
let db=da.map((d)=>{return Utilities.formatDate(new Date(d),Session.getScriptTimeZone(),"MMM dd")});
Logger.log(db);
}
Execution log
12:28:30 PM Notice Execution started
12:28:30 PM Info [Feb 09, Feb 10, Feb 11, Feb 12, Feb 13]
12:28:30 PM Notice Execution completed
Upvotes: 0
Reputation: 4510
Instead of copying references of dates and modifying them in place, create a new one each time, for example:
const numDays = 5
const oneDayOffset = 24 * 3600 * 1000;
const dates = [];
const startDate = new Date();
for (let i=0; i < numDays; i++){
const newDate = new Date( startDate.getTime() + i * oneDayOffset );
dates.push(newDate.toString())
}
console.log(dates)
Upvotes: 0
Reputation: 321
When you do
Billing_Dates.push(Current_Date)
you are pushing the reference to the date, not the date itself. So on the next loop, you change the date and anything referencing it changes as well. That's why the last two dates in your array always appear the same. In this line
Current_Date = new Date(Current_Date.setHours(Current_Date.getHours() + 24));
You create a new reference by doing new Date(), which is why it's only modifying the last two elements in your array.
To fix, create a new reference when you store in the array :
Billing_Dates.push(new Date(Current_Date))
Upvotes: 1