try below code
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) But result shows as below
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) ] When I console the 'startDate' just after the array push it shows as expected , But when I console log the time array it has been shifted . Can someone explain this?
startDate =new Date(startDate.setMonth(startDate.getMonth()+1))You will get the point