-3

I am getting an array of dates in this format

Sat Feb 12 2022 01:00:00 GMT+0100 (West Africa Standard Time),Sun Feb 13 2022 01:00:00 GMT+0100 (West Africa Standard Time),Mon Feb 14 2022 01:00:00 GMT+0100 (West Africa Standard Time)

But i want it in a format like this

Sat, Feb 12, 2022,
Sun, Feb 13, 2022,
Mon, Feb 14, 2022

This is the code that output my date

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

  while (currentDate <= new Date(endDate)) {
    dateArray.push(new Date(currentDate));
    currentDate.setUTCDate(currentDate.getUTCDate() + steps);
  }

  return dateArray;
}
var currD = new Date().toISOString().slice(0,10);

function addDays(date, days) {
  var result = new Date(date);
  result.setDate(result.getDate() + days);
  return result;
}
 const futureDate = addDays(currD, 3);

const dates = dateRange(currD, futureDate);
console.log(dates);
alert(dates)
RobG
  • 142,382
  • 31
  • 172
  • 209
  • Does this answer your question? [How to format a JavaScript date](https://stackoverflow.com/questions/3552461/how-to-format-a-javascript-date) – pilchard Feb 12 '22 at 21:21
  • Regarding the example of the output that you want: can you write that in JSON so as to show _exactly_ what you need? It's very ambiguous the way it's written now. – Nicolas Goosen Feb 12 '22 at 21:30
  • `dateArray.push(new Date(currentDate))` creates an array of Date objects. What you're seeing is likely console output where the dates have been stringified for display. Note that you're effectively using UTC for everything but the dates are displayed as local, hence why the time is equal to the offset. – RobG Feb 12 '22 at 21:34
  • @pilchard https://stackoverflow.com/questions/3552461/how-to-format-a-javascript-date . This was helpful. Thank you. – user18191602 Feb 13 '22 at 10:59

1 Answers1

0

Try this

const dates = [
    'Sat Feb 12 2022 01:00:00 GMT+0100 (West Africa Standard Time)',
    'Sun Feb 13 2022 01:00:00 GMT+0100 (West Africa Standard Time)',
    'Mon Feb 14 2022 01:00:00 GMT+0100 (West Africa Standard Time)',
];

const formatted = dates.map((date) => new Date(date).toDateString());

console.log(formatted);
mstephen19
  • 1,733
  • 1
  • 5
  • 20