1

I am trying to find the weekdays from today to Sunday. Since today is Monday I want to display the dates from Monday till Sunday, but tomorrow, I want my programme works from Tuesday to Sunday.

dateSets() {
            let firstDayOfWeek = "";
            let dates = [];
            firstDayOfWeek = new Date();
                let sunday = new Date();
                sunday.setDate(sunday.getDate() - sunday.getDay() + 7);
                const diff = sunday.getDate() - firstDayOfWeek.getDate();
                dates.push(new Date());
                for (let i = 0; i < diff; i++) {
                    dates.push(
                        new Date(firstDayOfWeek.setDate(firstDayOfWeek.getDate() + 1))
                    );
                }
            return dates;
        },

And here is the other function to find the date of the week:

getDateOfWeek(week, year) {
            let simple = new Date(year, 0, 1 + (week - 1) * 7);
            let dow = simple.getDay();
            let weekStart = simple;
            if (dow <= 4) weekStart.setDate(simple.getDate() - simple.getDay() + 1);
            else weekStart.setDate(simple.getDate() + 8 - simple.getDay());
            return weekStart;
        },

But it doesn't work that I expected, in dataset, only Monday is being displayed but not other dates and I don't understand the reason. If you can help me with this, I would be really glad. Thanks...

magic bean
  • 787
  • 12
  • 39
  • [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173) and [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – VLAZ Sep 27 '21 at 13:19
  • You want WeekDays like `Monday, Tuesdays ...` or their dates like `27/09/2021`? – navnath Sep 27 '21 at 13:49
  • @navnath like this 27/09/2021 but my problem is for this week, it only shows Monday and I think it is because he month is changing in this week. – magic bean Sep 27 '21 at 13:51
  • @RobG yes I guessed bout it but couldn't do could you show me how to do it? – magic bean Sep 27 '21 at 14:05

4 Answers4

1

This is kind of your choice but if you install NodeJS and open your folder in cmd and type in 'npm init' and 'npm I days' and open your editor and type in

var days = require('days');
console.log(days); // ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]

console will reveal all of the days of the week and if you want a specific day you can do the following

var days = require('days');
console.log(days[0]);  //Sunday

if you need help with installing NodeJS watch a YouTube video or reply to this comment I will help

Aqua
  • 37
  • 8
1
let firstDayOfWeek = "";
let dates = [];
firstDayOfWeek = new Date();
    let sunday = new Date();
    sunday.setDate(sunday.getDate() - sunday.getDay() + 7);
    //const diff = sunday.getDate() - firstDayOfWeek.getDate();
    //Try this code
    var timeDiff = Math.abs(sunday.getTime() - firstDayOfWeek.getTime());
    var diff = Math.ceil(timeDiff / (1000 * 3600 * 24)); 
    dates.push(new Date());
    for (let i = 0; i < diff; i++) {
        dates.push(
            new Date(firstDayOfWeek.setDate(firstDayOfWeek.getDate() + 1))
        );
    }
return dates;
1

function getWeekDates(){
  const dates = [new Date()]; // today
  const curr = new Date();
  const remDaysCount = 7-curr.getDay();
  for(let i=1; i<= remDaysCount; i++){
    // increase current Date by 1 and set to current Date
      const nextDate = curr.setDate(curr.getDate()+1);
    dates.push(new Date(nextDate));
  }
  return dates;
}

console.log(getWeekDates());
navnath
  • 3,548
  • 1
  • 11
  • 19
1

Your issue is here:

const diff = sunday.getDate() - firstDayOfWeek.getDate()

Currently the date is 27 Sep and next Sunday is 3 Oct so diff is -4 and the for loop test i < diff is false from the start. Consider using a loop and increment the date from today until it gets to Sunday.

function getDaysToSunday(date = new Date()) {
  let d = new Date(+date);
  let result = [];
  do {
    result.push(new Date(+d));
    d.setDate(d.getDate() + 1);
  } while (d.getDay() != 1)
  return result;
}

console.log(getDaysToSunday().map(d=>d.toDateString()));
RobG
  • 142,382
  • 31
  • 172
  • 209