Get Date Range Between Two Given Dates Using Moment.js
Introduction
Working with date ranges is a common task in web development, and the Moment.js library simplifies this process. In this article, we’ll explore how to get a date range between two given dates using Moment.js. We’ll provide a practical JavaScript function and a code example to demonstrate how this can be achieved effortlessly.
Moment.js is a widely used JavaScript library for parsing, manipulating, and formatting dates and times. Although it’s no longer actively maintained as of September 2020, it is still a valuable tool for handling dates in many projects.
Getting Started
To start working with Moment.js and retrieve a date range, you need to have the library installed in your project. You can add it via npm or include it directly from a CDN in your HTML file. Once Moment.js is set up, you can use it to create a function that generates a date range.
Creating the getDateRange
Function
Let’s break down the getDateRange
function:
const getDateRange = (firstDate, lastDate) => {
if (moment(firstDate, 'YYYY-MM-DD').isSame(moment(lastDate, 'YYYY-MM-DD'), 'day'))
return [lastDate];
let date = firstDate;
const dates = [date];
do {
date = moment(date).add(1, 'day');
dates.push(date.format('YYYY-MM-DD'));
} while (moment(date).isBefore(lastDate));
return dates;
};
- We define a function called
getDateRange
that takes two parameters:firstDate
andlastDate
. These parameters represent the start and end dates of the desired date range. - We check if
firstDate
andlastDate
are the same day using the.isSame()
method provided by Moment.js. If they are the same, we return an array containing onlylastDate
since the range consists of a single date. - If the dates are not the same, we initialize a
date
variable with the value offirstDate
, and we create an empty array calleddates
to store the range. - We enter a
do-while
loop that continues untildate
is no longer beforelastDate
. In each iteration, we incrementdate
by one day using.add(1, 'day')
and format it to the 'YYYY-MM-DD' format using.format()
. We then push this formatted date into thedates
array. - Finally, we return the
dates
an array containing the complete date range.
Using the getDateRange
Function
To use the getDateRange
function and see it in action, you can create a simple script as follows:
(async () => {
const dates = getDateRange('2023-08-28', '2023-10-05');
console.log('dates: ', dates);
})();
In this script, we call the getDateRange
function with the start date '2023-08-28' and the end date '2023-10-05'. The resulting date range is then logged to the console.
Conclusion
Moment.js remains a valuable tool for working with dates and times in JavaScript, despite its deprecation. With the getDateRange
function we've created, you can easily generate date ranges between two given dates. This can be particularly useful in various applications, from scheduling to data analysis, where you need to work with date intervals.
Remember that while Moment.js is still functional and widely used, you may also consider modern alternatives like the date-fns or Luxon libraries, which offer similar functionality and better support for modern JavaScript development practices.