Check out the code snippet below:
let startDate = new Date();
const lastDate = new Date();
lastDate.setMonth(11,31);
const timeArray = [];
while(startDate <lastDate){
timeArray.push(startDate);
console.log(startDate)
startDate =new Date(startDate.setMonth(startDate.getMonth()+1))
}
console.log('==================================');
console.log(timeArray)
However, the resulting output displays:
Thu Nov 03 2022 09:12:03 GMT+0530 (India Standard Time)
Sat Dec 03 2022 09:12:03 GMT+0530 (India Standard Time)
==================================
[
Sat Dec 03 2022 09:12:03 GMT+0530 (India Standard Time)
Tue Jan 03 2023 09:12:03 GMT+0530 (India Standard Time)
]
After pushing 'startDate' into the array and checking it immediately, it appears as expected. However, when logging the time array itself, the dates seem to have shifted. Can anyone provide an explanation for this behavior?