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
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Date
Date - JavaScript | MDN - Mozilla
The following examples show how to determine the elapsed time between two JavaScript dates in milliseconds. Due to the differing lengths of days (due to daylight saving changeover), months, and years, expressing elapsed time in units greater than hours, minutes, and seconds requires addressing a number of issues, and should be thoroughly researched before being attempted. ... // Using Date objects const start = Date.now(); // The event to time goes here: doSomethingForALongTime(); const end = Date.now(); const elapsed = end - start; // elapsed time in milliseconds
Discussions

node.js - Javascript - get array of dates between 2 dates - Stack Overflow
I'd like "range" to be an array of date objects, one for each day between the two dates. More on stackoverflow.com
🌐 stackoverflow.com
Passing an array to the Javascript Date constructor, is it standard? - Stack Overflow
Seems to be non-standard, probably ...la.org/en/JavaScript/Reference/Global_Objects/… ... The ES5 spec details the new Date(value) form of the Date constructor. In the algorithm for handling this form, value is converted to a primitive value by calling the [[DefaultValue]] internal method of the object. Converting an array to a primitive ... More on stackoverflow.com
🌐 stackoverflow.com
How to format a date that is a value in array of objects
I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read · You can also use the “preformatted text” tool in the editor ... More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
1
0
October 21, 2021
How to get minimum date from array of object
activities.filter(el => el.date) This would give you only the objects that don't have 'null' for the date property. More on reddit.com
🌐 r/learnjavascript
5
1
October 25, 2021
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Date › Date
Date() constructor - JavaScript - MDN Web Docs - Mozilla
July 10, 2025 - Given at least a year and month, this form of Date() returns a Date object whose component values (year, month, day, hour, minute, second, and millisecond) all come from the following parameters. Any missing fields are given the lowest possible value (1 for day and 0 for every other component).
🌐
W3Schools
w3schools.com › js › js_dates.asp
JavaScript Dates
February 4, 2026 - Object Study Path Object Intro Object Properties Object Methods Object this Object Display Object Constructors JS Scope · JS Scope JS Code Blocks JS Hoisting JS Strict Mode JS Dates · JS Dates JS Date Formats JS Date Get JS Date Set JS Date Methods JS Temporal New · Temporal Study Path Temporal Intro Temporal vs Date Temporal Duration Temporal Now Temporal Instant Temporal PlainDate Temporal PlainTime Temporal PlainDateTime Temporal ZonedDateTime Temporal Arithmetic Temporal Migrate Temporal Formats Temporal Reference JS Arrays
🌐
W3Schools
w3schools.com › js › js_date_methods.asp
JavaScript Get Date Methods
JSON Intro JSON Syntax JSON vs ... Prep JS Bootcamp JS Certificate JS Reference ... In JavaScript, date objects are created with new Date()....
Find elsewhere
🌐
W3Schools
w3schools.com › js › js_arrays.asp
JavaScript Arrays
Object Study Path Object Intro Object Properties Object Methods Object this Object Display Object Constructors JS Scope · JS Scope JS Code Blocks JS Hoisting JS Strict Mode JS Dates · JS Dates JS Date Formats JS Date Get JS Date Set JS Date Methods JS Temporal New · Temporal Study Path Temporal Intro Temporal vs Date Temporal Duration Temporal Now Temporal Instant Temporal PlainDate Temporal PlainTime Temporal PlainDateTime Temporal ZonedDateTime Temporal Arithmetic Temporal Migrate Temporal Formats Temporal Reference JS Arrays
🌐
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.
🌐
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?
February 16, 2023 - </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>
🌐
W3Schools
w3schools.com › jsref › jsref_obj_date.asp
JavaScript Date Reference
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING INTRO TO HTML & CSS BASH RUST ... Array[ ] Array( ) at() concat() constructor copyWithin() entries() every() fill() filter() find() findIndex() findLast() findLastIndex() flat() flatMap() forEach() from() includes() indexOf() isArray() join() keys() lastIndexOf() length map() of() pop() prototype push() reduce() reduceRight() rest (...) reverse() shift() slice() some() sort() splice() spread (...) toReversed() toSorted() toSpliced() toString() unshift() values() valueOf() with() JS Boolean
🌐
W3Resource
w3resource.com › javascript-exercises › javascript-date-exercise-11.php
JavaScript: Maximum date from an array of dates - w3resource
July 17, 2025 - 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 ...
🌐
W3Resource
w3resource.com › javascript › object-property-method › date.php
Javascript Date Objects - Properties and Methods - w3resource
new Date(year, month, day, hours, minutes, seconds, milliseconds). [for example inauguration_day = new Date(97,8,15,10,05,0) ]. ... JavaScript Core objects, methods, properties. Previous: JavaScript valueOf() Method: Array Object Next: JavaScript constructor Property: Date Object
🌐
Webdevtutor
webdevtutor.net › blog › javascript-date-array
Mastering JavaScript Date Arrays: A Comprehensive Guide
To create a date array in JavaScript, you can use the Date object to represent individual dates and then store them in an array.
🌐
University of Washington
cev.washington.edu › lc › CLWEBCLB › jst › js_datetime.html
JavaScript Date Object
HTML,CSS,JavaScript,DHTML,XML,XHTML,ASP,ADO and VBScript tutorial from W3Schools.
🌐
Medium
fireflysemantics.medium.com › searching-an-array-of-dates-with-javascript-aa8e8708708d
Searching an Array of Dates with Javascript - Ole Ersoy - Medium
October 26, 2020 - Searching an Array of Dates with Javascript Scenario We have a text input="11/25/2014" . We want to see whether the input matches any of the dates in the array: const dates = [new Date(2014, 11, 25) …
🌐
DEV Community
dev.to › askyt › sorting-an-array-of-objects-by-date-in-javascript-3pnb
Sorting an Array of Objects by Date in JavaScript - DEV Community
January 7, 2025 - const data = [ { name: 'Event 1', date: new Date('2023-01-15') }, { name: 'Event 2', date: new Date('2022-12-20') }, { name: 'Event 3', date: new Date('2023-03-05') } ]; data.sort((a, b) => a.date.getTime() - b.date.getTime()); console.log(data);
🌐
Mastering JS
masteringjs.io › tutorials › fundamentals › sort-by-date
How to Sort an Array by Date in JavaScript - Mastering JS
October 14, 2021 - const d1 = new Date('2019-06-01'); const d2 = new Date('2018-06-01'); const d3 = new Date('2019-06-01'); const objects = [ { createdAt: d1, name: 'Test 1' }, { createdAt: d2, name: 'Test 2' }, { createdAt: d3, name: 'Test 3' } ]; objects.sort((a, b) => a.createdAt - b.createdAt); // [ 'Test 2', 'Test 1', 'Test 3' ] console.log(objects.map(o => o.name)); You could also sort without using the time portion of the date.
🌐
javaspring
javaspring.net › blog › how-to-sort-array-by-date-in-javascript
How to Sort an Array of Objects by Date in JavaScript: Step-by-Step Guide with Examples — javaspring.net
Use Array.sort() with a comparator that converts dates to Date objects (or timestamps) and returns their difference. Handle custom date formats with explicit parsing (native or via libraries like date-fns).