如何格式化日期数组



我得到了一个格式的日期数组

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)

但我想要像这样的格式

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

这是输出我的日期的代码

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)

试试这个

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);

相关内容

  • 没有找到相关文章

最新更新