-2

I tried to make an array which contains data of 1 week, but can't figure something else than this

Should I do a for loop or something? I am quite noob, so I hardcode it to make it work. The code does work but it is kinda slow and weird, I think there is another solution to this, help me please lol

const days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
let today, date;
  

today = new Date();
  date = new Date(today);
  const one = new Date();
  one.setDate(date.getDate() + 1);
  const two = new Date();
  two.setDate(date.getDate() + 2);
  const three = new Date();
  three.setDate(date.getDate() + 3);
  const four = new Date();
  four.setDate(date.getDate() + 4);
  const five = new Date();
  five.setDate(date.getDate() + 5);
  const six = new Date();
  six.setDate(date.getDate() + 6);

  const dataday = [
    {
      day: days[today.getDay()],
      num: today.getDate(),
      date:
        today.getDate() + "-" + today.getFullYear() + "-" + today.getMonth(),
      selected: false,
    },
    {
      day: days[one.getDay()],
      num: one.getDate(),
      date: one.getDate() + "-" + one.getFullYear() + "-" + one.getMonth(),
      selected: false,
    },
    {
      day: days[two.getDay()],
      num: two.getDate(),
      date: two.getDate() + "-" + two.getFullYear() + "-" + two.getMonth(),
      selected: false,
    },
    {
      day: days[three.getDay()],
      num: three.getDate(),
      date:
        three.getDate() + "-" + three.getFullYear() + "-" + three.getMonth(),
      selected: false,
    },
    {
      day: days[four.getDay()],
      num: four.getDate(),
      date: four.getDate() + "-" + four.getFullYear() + "-" + four.getMonth(),
      selected: false,
    },
    {
      day: days[five.getDay()],
      num: five.getDate(),
      date: five.getDate() + "-" + five.getFullYear() + "-" + five.getMonth(),
      selected: false,
    },
    {
      day: days[six.getDay()],
      num: six.getDate(),
      date: six.getDate() + "-" + six.getFullYear() + "-" + six.getMonth(),
      selected: false,
    },
  ];

  console.log(dataday);
RobG
  • 142,382
  • 31
  • 172
  • 209
henry
  • 47
  • 1
  • 6

1 Answers1

0

I answered a similar question here, just slightly modified it for you:

  • create today's date with new Date();
  • grab the day (d), month (m), and year (y) in a temp object
  • check how many days this month has (like September has 30)
  • create an array of length 7 and create each object of the array
  • as you create each object, increment your temp.d by 1
  • if you exceed the number of days in the month, add 1 to temp.m and reset temp.d to 1 (start of a new month)
  • do your custom object representation you want

console.log(getThisWeek());

function getThisWeek() {
  var today = new Date();
  const temp = {
    d: today.getDate(),
    m: today.getMonth(),
    y: today.getFullYear(),
  }
  const numDaysInMonth = new Date(temp.y, temp.m + 1, 0).getDate()

  return Array.from({length: 7}, _ => {
    if (temp.d > numDaysInMonth){
      temp.m +=1;
      temp.d = 1;
      // not needed, Date(2020, 12, 1) == Date(2021, 0, 1)
      /*if (temp.m >= 12){
        temp.m = 0
        temp.y +=1
      }*/
    }      

    const newDate = new Date(temp.y, temp.m, temp.d++); //.toUTCString()

    return {
      day: newDate.toLocaleDateString('en-US', {weekday: 'short'}),
      num: newDate.getDate(),
      date: newDate.getDate() + "-" + newDate.getFullYear() + "-" + newDate.getMonth(),
      selected: false,
    };
  });
}
Alex L
  • 4,168
  • 1
  • 9
  • 24