Skip to main content

How to Get All Dates Between Two Dates in JavaScript

Generating a list of dates within a specific range is a common requirement for building calendars, timelines, or reports. For example, you might need to get all the days in a given month or find the dates for the past week.

This guide will teach you how to create a single, powerful, and reusable function to generate an array of Date objects between a start and end date. You will then learn how to use this function to solve more specific problems, like getting all the days in a month.

The Core Method: A Reusable getDatesInRange Function

The most effective way to handle date ranges is to create a function that takes a start date and an end date and returns an array of all the dates in between.

The logic:

  1. Start with a given startDate.
  2. Create a loop that continues as long as the current date is less than or equal to the endDate.
  3. Inside the loop, add the current date to an array.
  4. Increment the current date by one day.
  5. Return the array of dates.

The reusable function (this will be used in next examples!)

/**
* Generates an array of Date objects between a start and end date (inclusive).
* @param {Date} startDate The start of the date range.
* @param {Date} endDate The end of the date range.
* @returns {Date[]} An array of Date objects.
*/
function getDatesInRange(startDate, endDate) {
// We clone the start date so we don't mutate the original object.
const date = new Date(startDate.getTime());
const dates = [];

while (date <= endDate) {
dates.push(new Date(date));
date.setDate(date.getDate() + 1);
}

return dates;
}

The solution that uses the getDatesInRange() function:

const startDate = new Date('2023-10-25');
const endDate = new Date('2023-10-29');

const dateRange = getDatesInRange(startDate, endDate);

console.log(dateRange);

The output is an array of 5 Date objects from Oct 25 to Oct 29.

How the function works:

  • new Date(startDate.getTime()): We create a clone of the startDate. This is a crucial best practice to avoid mutating the original startDate object that was passed into the function, which would be an unexpected side effect.
  • while (date <= endDate): This condition works because the < and > operators implicitly convert the Date objects to their numeric timestamp values for comparison.
  • dates.push(new Date(date)): Inside the loop, we push another clone of the current date into our results array.
  • date.setDate(date.getDate() + 1): This is the key to the iteration. It increments the date variable by one day. The setDate method automatically handles rolling over to the next month or year.

Practical Use Cases

With our getDatesInRange function, solving more specific problems becomes incredibly simple.

How to Get All Dates in a Specific Month

To get all the days in a month, you just need to find the first and last day of that month and pass them to our reusable function.

function getAllDaysInMonth(year, month) {
// The month is 0-indexed (0 for January)
const startDate = new Date(year, month, 1);
// Get the last day by going to the first day of the NEXT month and subtracting one day.
const endDate = new Date(year, month + 1, 0);

return getDatesInRange(startDate, endDate);
}

// Example: Get all days in October 2023
const octoberDates = getAllDaysInMonth(2023, 9); // 9 = October
console.log(octoberDates);

The output is an array of 31 Date objects.

How to Get the Dates for the Past 7 Days

To get a rolling list of recent dates, you just need to define "today" and "a week ago" as your range.

function getPast7Days() {
const today = new Date();
const sevenDaysAgo = new Date();
sevenDaysAgo.setDate(today.getDate() - 6); // Set to 6 days ago to include today

// Ensure the start date is the earlier one
return getDatesInRange(sevenDaysAgo, today);
}

// Example Usage:
const lastWeek = getPast7Days();
console.log(lastWeek);

The output is an array of 7 Date objects, ending with today.

Conclusion

Generating a range of dates is a common task that can be solved cleanly with a single, robust function.

  • The core logic involves a while loop that iterates from a startDate to an endDate, incrementing a Date object by one day in each step using setDate().
  • The best practice is to encapsulate this logic in a reusable getDatesInRange() function.
  • This one utility function can then be easily adapted to solve more specific problems, like getting all days in a month or a rolling week, leading to cleaner and more maintainable code.