How to get all the dates between two dates in JavaScript?

We have introduced three methods in JavaScript to get all the dates between two dates: using loops, using built-in functions, and using third-party libraries. Each method has its pros and cons, and we need to choose the appropriate method based on the specific situation to handle the date range.

Home > Blog > How to get all the dates between two dates in JavaScript?

In daily web development, we often need to deal with date-related tasks, such as calculating the number of days between two dates, displaying data within a date range, etc. Today, we will discuss a common problem: how to get all dates between two dates in JavaScript?

Before we start, let's first understand some basic knowledge. In JavaScript, dates can be represented by Date objects, which have various methods and properties that can easily manipulate and calculate dates. Let's take a look at the methods that can solve the problems in this article.

Method 1 : Use loops

Let's first look at a more basic solution: using a loop to gradually traverse the date range. This method is more intuitive. We can start from the start date and increase it by one day until we reach the end date. The reference code is as follows:

function getDatesBetween(startDate, endDate) {
  let dates = [];
  let currentDate = new Date(startDate);

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

  return dates;
}

const startDate = new Date('2024-01-01');
const endDate = new Date('2024-01-05');
console.log(getDatesBetween(startDate, endDate));
get all the dates between two dates in JavaScript

Method 2 : Use built-in functions

We can use the built-in functions in JavaScript to optimize the process of obtaining date ranges. With the built-in functions of Array, we can generate date ranges more concisely. The example code is as follows:

function getDatesBetween(startDate, endDate) {
  const days = Math.floor((endDate - startDate) / (24 * 60 * 60 * 1000)) + 1;
  return Array.from({ length: days }, (_, index) => {
    const newDate = new Date(startDate);
    newDate.setDate(startDate.getDate() + index);
    return newDate;
  });
}

const startDate = new Date('2024-01-01');
const endDate = new Date('2024-01-05');
console.log(getDatesBetween(startDate, endDate));
get all the dates between two dates in JavaScript

Method 3 : Use third-party libraries

In addition to the above two methods, we can also consider using a third-party date processing library to simplify the operation. For example, we can use a library like Moment.js to handle date ranges (remember to introduce it in HTML), the example code is as follows:

// Import Moment.js
// <script src="https://cdn.jsdelivr.net/npm/moment/moment.min.js"></script>

// Using Moment.js
const startDate = moment('2024-01-01');
const endDate = moment('2024-01-05');
const dates = [];

// Loop to generate date range
while (startDate.isSameOrBefore(endDate)) {
  dates.push(startDate.clone());
  startDate.add(1, 'days');
}

// Format and output the array of dates
console.log("Date Range:");
dates.forEach(date => {
  console.log(date.format('YYYY-MM-DD'));
});
get all the dates between two dates in JavaScript

Performance considerations

When choosing a method, we also need to consider performance issues. For a large range of dates, iterative traversal may be time-consuming, so we can consider using built-in functions or third-party libraries to improve efficiency.

Summary

In this article, we introduced three methods to obtain all dates between two dates in JavaScript: using loops, using built-in functions, and using third-party libraries. Each method has its own advantages and disadvantages, and we need to choose the appropriate method based on the specific situation to handle date ranges.

Reference link:

How to get all the dates between two dates in JavaScript?
We have introduced three methods in JavaScript to get all the dates between two dates: using loops, using built-in functions, and using third-party libraries. Each method has its pros and cons, and we need to choose the appropriate method based on the specific situation to handle the date range.

Learn more: