make a date range from today to another date in javascript

To create a date range from today to another date in JavaScript, we can use the moment.js library to easily work with dates. Here's an example code snippet:

index.tsx
const today = moment()
const endDate = moment('2022-12-31') // replace with your desired end date

// create an array of dates between today and the end date
const dateRange = []
while (today.isSameOrBefore(endDate)) {
  dateRange.push(today.format('YYYY-MM-DD'))
  today.add(1, 'day')
}

console.log(dateRange) // an array of dates in 'YYYY-MM-DD' format
356 chars
12 lines

In this code, we first create a moment object for today's date, and another moment object for the desired end date (in this case, December 31st, 2022).

Next, we create an empty array to store the dates in our range. We then use a while loop to iterate through each day between today and the end date. Inside the loop, we use the format method to convert each date to a string in the format 'YYYY-MM-DD', and push it onto our dateRange array.

Finally, we log the dateRange array to the console, which will contain an array of dates in the desired range.

gistlibby LogSnag