i want to get the list of next week days in given format "March 12 Sunday" and then convert that is final list of week days as given below.
Here is a my current code with which i am trying to get desire output but that returns "06/12/22" format..
Current code :
const nextWeek = [...Array(7).keys()].map(days => new Date(Date.now() + 86400000 * days).toLocaleDateString('en-us', { weekday:"long", month:"short", day:"numeric"}))
console.log("== > ",nextWeek)
current output :
["09/17/22", "09/18/22", "09/19/22", "09/20/22", "09/21/22", "09/22/22", "09/23/22"]
first i want this output
["Sunday, March 4", "Monday, March 4", "Tuesday, March 4", "Wednesday, March 4", "Thursday, March 4", "Friday, March 4", "Saturday, March 4"]
and then final desire output is:
const nextWeekdata = [
{ id: 1, name: "Sunday" ,date:21,Month:"March" },
{ id: 2, name: "Monday" ,date:22,Month:"March" },
{ id: 3, name: "Tuesday" ,date:23,Month:"March" },
{ id: 4, name: "Wednesday" ,date:24,Month:"March" },
{ id: 5, name: "Thursday" ,date:25,Month:"March" },
{ id: 6, name: "Friday" ,date:26,Month:"March" },
{ id: 7, name: "Saturday" ,date:27,Month:"March" },
];
>Solution :
const nextWeek = [...Array(7).keys()].map((days) =>
new Date(Date.now() + 86400000 * days).toLocaleDateString("en-us", {
weekday: "long",
month: "short",
day: "numeric",
})
);
let newArray = [];
nextWeek.forEach((item, index) => {
let data = item.split(",");
let secondLength = data[1].length;
newArray.push({
id: index + 1,
name: data[0],
date: data[1].substring(secondLength - 2),
month: data[1].substring(1, secondLength - 3),
});
});
console.log("days", newArray);
does this work? yes
is this the right way to do it? nope
is this the easy way? for me, it is.