JavaScript provides several ways to generate an array containing all dates between a given start date and end date. These approaches increment the date one day at a time and store each date in a new array.
- Start with the given start and end dates.
- Increment the current date by one day until it reaches the end date.
- Store each generated date in an array using methods such as push() or concat().
Approach 1: Using push() Method
In this approach, start with the first date and continue adding one day until the current date reaches the end date. The push() method is used to store each date in the result array.
Example: Stores all dates between the given start and end dates in an array.
Date.prototype.addDay = function(days) {
let date = new Date(this.valueOf());
date.setDate(date.getDate() + days);
return date;
};
function getDate(strDate, stpDate) {
let dArray = new Array();
let cDate = strDate;
while (cDate <= stpDate) {
// Adding the date to the array
dArray.push(new Date(cDate));
// Increment the date by 1 day
cDate = cDate.addDay(1);
}
return dArray;
}
function GFG_Fun() {
let startDate = new Date();
// Making endDate equal to 4 more days
// from startDate
let endDate = startDate.addDay(4);
console.log(getDate(startDate, endDate));
}
GFG_Fun();
Output
[ 2026-08-29T07:36:15.819Z, 2026-08-30T07:36:15.819Z, 2026-08-31T07:36:15.819Z, 2026-09-01T07:36:15.819Z, 2026-09-02T07:36:15.819Z ]
Approach 2: Using for Loop and push() Method
In this approach, the difference between consecutive dates is represented in milliseconds. A for loop increments the timestamp by one day and adds each date to the result array.
Example: Generates all dates between two given dates using a for loop.
Date.prototype.addDay = function(days) {
let date = new Date(this.valueOf());
date.setDate(date.getDate() + days);
return date;
};
function getDates(date1, date2) {
let _1Day = 24 * 3600 * 1000;
// Array to store all dates
let dates = [];
for (let ms = date1.getTime(),
last = date2.getTime();
ms <= last;
ms += _1Day) {
dates.push(new Date(ms));
}
return dates;
}
function GFG_Fun() {
let startDate = new Date();
// Making endDate equal to 4 more days
// from startDate
let endDate = startDate.addDay(4);
console.log(getDates(startDate, endDate));
}
GFG_Fun();
Output
[ 2026-08-29T07:36:15.785Z, 2026-08-30T07:36:15.785Z, 2026-08-31T07:36:15.785Z, 2026-09-01T07:36:15.785Z, 2026-09-02T07:36:15.785Z ]
Approach 3: Using concat() Method
The concat() method can be used to add each generated date to a new array. A loop starts from the given start date and increments it by one day until the end date is reached.
Example: Uses concat() to store all dates between two given dates.
function getAllDates(startDate, endDate) {
let dates = [];
for (let date = new Date(startDate);
date <= endDate;
date.setDate(date.getDate() + 1)) {
dates = dates.concat(new Date(date));
}
return dates;
}
const startDate = new Date('2023-01-01');
const endDate = new Date('2023-01-10');
const result = getAllDates(startDate, endDate);
console.log(result);
Output
[ 2023-01-01T00:00:00.000Z, 2023-01-02T00:00:00.000Z, 2023-01-03T00:00:00.000Z, 2023-01-04T00:00:00.000Z, 2023-01-05T00:00:00.000Z, 2023-01-06T00:00:00.000Z, 2023-01-07T00:00:00.000Z, ...