The problem you have is that you push references to the date1 object. When you change the date on it in your loop, you update the object, but all references still point to the same object.

You need to either push a text representation of your date, or a copy of your date1 object

for (var i=0; date1 <= date2; date1.setDate(date1.getDate() + 1), i++) {
    alldates.push(new Date(date1));
    }

alert(alldates.join('\n'));

As suggested, with a while loop

while( date1 <= date2 ) {
  alldates.push(new Date(date1));
  date1.setDate( date1.getDate() +1 );
} 
Answer from jaudette on Stack Overflow
🌐
TutorialsPoint
tutorialspoint.com › how-to-store-all-dates-in-an-array-present-in-between-given-two-dates-in-javascript
How to store all dates in an array present in between given two dates in JavaScript?
</h2> <div id = "output"></div> <script> var output = document.getElementById('output'); var date1 = new Date("2023-01-01"); var date2 = new Date("2023-01-11"); output.innerHTML += "The date1 is " + date1 + "<br/>"; output.innerHTML += "The date2 is " + date2 + "<br/>"; var dateArray = []; while (date1 <= date2) { dateArray.push(new Date(date1)); date1.setDate(date1.getDate() + 1); } output.innerHTML += "The date array is <br/>"; for (let i = 0; i < dateArray.length; i++) { output.innerHTML += dateArray[i] + " <br/>"; } </script> </body> </html> In this approach, we will get the total milliseconds of the first and second dates. After that, we will keep adding the milliseconds of 1 day to the total milliseconds of the current date and using new milliseconds, and we can create a date. This way, we can find all dates between the given two dates and store them in the array.
🌐
Webdevtutor
webdevtutor.net › blog › javascript-date-in-array
Working with JavaScript Date Objects in Arrays
javascript let filteredDates = dateArray.filter(date => date < new Date('2025-03-17')); Manipulating dates within an array is also possible using methods like map or forEach. These methods allow you to iterate over the array and perform operations on each date object.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-store-all-dates-in-an-array-present-in-between-given-two-dates-in-javascript
How to store all dates in an array present in between given two dates in JavaScript ? - GeeksforGeeks
July 3, 2024 - The function getAllDates uses a for loop to iterate through dates between startDate and endDate, incrementing by one day each iteration. Dates are then concatenated into an array.
🌐
Stack Overflow
stackoverflow.com › questions › 32936673 › using-the-date-object-in-an-array-with-javascript
Using the Date Object in an Array with Javascript - Stack Overflow
I need just the last input to increment a day and I thought putting it in an array would work but now nothing displays. Thanks for helping. var lowTempArray = []; var highTempArray = []; var nowArray = []; function process() { 'use strict'; //declare variables var lowTemp = document.getElementById('lowTemp'); var highTemp = document.getElementById('highTemp'); var output = document.getElementById('output'); var date = new Date(); var message = ' '; if (lowTemp.value) { lowTempArray.push(lowTemp.value); if (highTemp.value) { highTempArray.push(highTemp.value); message = '<table><th>Date&nbsp;&n
🌐
W3Schools
w3schools.com › js › js_arrays.asp
JavaScript Arrays
An array can hold many values under a single name, and you can access the values by referring to an index number. Using an array literal is the easiest way to create a JavaScript Array.
Find elsewhere
🌐
Webdevtutor
webdevtutor.net › blog › javascript-date-range-array
Working with Date Range Arrays in JavaScript
To create a date range array in JavaScript, you can use the following code snippet: javascript const startDate = new Date('2025-03-01'); const endDate = new Date('2025-03-15');
🌐
W3Schools
w3schools.com › js › js_dates.asp
JavaScript Dates
Specifying a month higher than ... one parameter it will be treated as milliseconds. ... JavaScript stores dates as number of milliseconds since January 01, 1970....
🌐
W3Resource
w3resource.com › javascript-exercises › javascript-date-exercise-11.php
JavaScript: Maximum date from an array of dates - w3resource
The code defines a JavaScript function named "max_date()" with one parameter 'all_dates', representing an array of date strings. ... It initializes two variables: 'max_dt' to store the maximum date string found so far and 'max_dtObj' to store ...
🌐
Stack Overflow
stackoverflow.com › questions › 27174649
javascript - Set array based on specific dates - Stack Overflow
You can index your compliments ... ¬¬"] }; console.log(compliments[today_string] || compliments["default"]]); ... Use objects line array....