Answers for "convert a date range into an array of date in js"

0

convert a date range into an array of date in js

function dateRange(startDate, endDate, steps = 1) {
  const dateArray = [];
  let currentDate = new Date(startDate);

  while (currentDate <= new Date(endDate)) {
    dateArray.push(new Date(currentDate));
    // Use UTC date to prevent problems with time zones and DST
    currentDate.setUTCDate(currentDate.getUTCDate() + steps);
  }

  return dateArray;
}

const dates = dateRange('2020-09-27', '2020-10-28');
console.log(dates);
Posted by: Guest on August-27-2021

Code answers related to "convert a date range into an array of date in js"

Code answers related to "Javascript"

Browse Popular Code Answers by Language