Home > OS >  how to get month, week , day format list of next week?
how to get month, week , day format list of next week?

Time:09-18

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" },
          ];

CodePudding user response:

    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.

CodePudding user response:

If you want to calculate the range of dates for next week, break your logic into smaller chunks.

You should have three functions:

  • Function that gets the date for a given day of the week; next week
  • Function that gets the range of all the dates next week
  • Function to map the dates to object data

const getNextDay = (dayIndex) => {
  const today = new Date();
  today.setDate(today.getDate()   (dayIndex - 1 - today.getDay()   7) % 7   1);
  return today;
};

const getNextWeek = () => [...Array(7).keys()].map(getNextDay);

const nextWeek = getNextWeek().map((date, index) => ({
  id: index   1,
  name: date.toLocaleDateString('en-us', { weekday: 'long' }),
  date: date.getDate(),
  month: date.toLocaleDateString('en-us', { month: 'long' }),
  year: date.getFullYear()
}));

console.log(nextWeek);
.as-console-wrapper { top: 0; max-height: 100% !important; }

CodePudding user response:

Actually, when I use your current code I get this as a result:

(7) ['Saturday, Sep 17', 'Sunday, Sep 18', 'Monday, Sep 19', 'Tuesday, Sep 20', 'Wednesday, Sep 21', 'Thursday, Sep 22', 'Friday, Sep 23']

Which means it works as you wish; what browser are you using?

Regarding the Second Question of yours, I'd have used something such as this:

class weekDay {
   constructer(id, x) {
      let name = x.split(", ")[0];
      let day = x.split(", ")[1].split(" ")[1];
      let month = x.split(", ")[1].split(" ")[0];
      this.id = id;
      this.name = name;
      this.day = day;
      this.month = month;
   }
}
const nextWeek = [...Array(7).keys()].map(days => new Date(Date.now()   86400000 * days).toLocaleDateString('en-us', { weekday:"long", month:"long", day:"numeric"}))
var arr = [];
for (let i=0; i<=6; i  ) {
   arr.push(new weekDay(i 1, nextWeek[i]));
}

This is my way of doing it, but take heed that I haven't tested it so it might have some syntax errors, if there was any, please notify me.

  • Related