0

I have a date set which I am filling it with the number of current week and year:

dateSets(week, year) {
            let fistDayOfTheWeek = '';
            if(this.currentWeekNumber === this.getWeekNumber(new Date())) {
                fistDayOfTheWeek = new Date();
            } else {
                fistDayOfTheWeek = this.getDateOfWeek(week, year);
            }
            let sunday = new Date(fistDayOfTheWeek);
            sunday.setDate(sunday.getDate() - sunday.getDay() + 7);
            const dates = [];
            const diff = sunday.getDate() - fistDayOfTheWeek.getDate();
            for (let i = 0; i <= diff; i++) {
                const upDate = new Date();
                upDate.setDate(fistDayOfTheWeek.getDate() + i);
                dates.push(upDate);
            }
            console.log(dates)
            return dates;
        },

So apperantly my dateSet function works like if it is not monday then show the dates from today to sunday and from next week from monday to sunday. But what is wrong in this function is it doesnt push when the month is changed. So for 4 weeks console.log(dates) displays:

  • [Tue Aug 10 2021 16:22:43 GMT+0200 (Central European Summer Time), Wed Aug 11 2021 16:22:43 GMT+0200 (Central European Summer Time), Thu Aug 12 2021 16:22:43 GMT+0200 (Central European Summer Time), Fri Aug 13 2021 16:22:43 GMT+0200 (Central European Summer Time), Sat Aug 14 2021 16:22:43 GMT+0200 (Central European Summer Time), Sun Aug 15 2021 16:22:43 GMT+0200 (Central European Summer Time)]

  • [Mon Aug 16 2021 16:22:46 GMT+0200 (Central European Summer Time),
    Tue Aug 17 2021 16:22:46 GMT+0200 (Central European Summer Time), Wed Aug 18 2021 16:22:46 GMT+0200 (Central European Summer Time), Thu Aug 19 2021 16:22:46 GMT+0200 (Central European Summer Time), Fri Aug 20 2021 16:22:46 GMT+0200 (Central European Summer Time), Sat Aug 21
    2021 16:22:46 GMT+0200 (Central European Summer Time), Sun Aug 22
    2021 16:22:46 GMT+0200 (Central European Summer Time)]

  • [Mon Aug 23 2021 16:22:47 GMT+0200 (Central European Summer Time),
    Tue Aug 24 2021 16:22:47 GMT+0200 (Central European Summer Time), Wed Aug 25 2021 16:22:47 GMT+0200 (Central European Summer Time), Thu Aug 26 2021 16:22:47 GMT+0200 (Central European Summer Time), Fri Aug 27 2021 16:22:47 GMT+0200 (Central European Summer Time), Sat Aug 28
    2021 16:22:47 GMT+0200 (Central European Summer Time), Sun Aug 29
    2021 16:22:47 GMT+0200 (Central European Summer Time)]

  • []

As you see since after 3 weeks, the month will be changed to september and I think that's why it comes to an empty array. I dont know if it is necessary but in any case here are the other functions that I used:

getDateOfWeek(w, y) {
            var simple = new Date(y, 0, 1 + (w - 1) * 7);
            var dow = simple.getDay();
            var ISOweekStart = simple;
            if (dow <= 4)
                ISOweekStart.setDate(simple.getDate() - simple.getDay() + 1);
            else
                ISOweekStart.setDate(simple.getDate() + 8 - simple.getDay());
            return ISOweekStart;
        }

getWeekNumber(date) {
            const temp_date = new Date(date.valueOf());
            const dayn = (date.getDay() + 6) % 7;
            temp_date.setDate(temp_date.getDate() - dayn + 3);
            const firstThursday = temp_date.valueOf();
            temp_date.setMonth(0, 1);
            if (temp_date.getDay() !== 4)
            {
                temp_date.setMonth(0, 1 + ((4 - temp_date.getDay()) + 7) % 7);
            }
            return 1 + Math.ceil((firstThursday - temp_date) / 604800000);
        },

PS: currentWeekNumber is increasing everytime when the button is clicked.

magic bean
  • 787
  • 12
  • 39

2 Answers2

0

Your issue is here:

const diff = sunday.getDate() - fistDayOfTheWeek.getDate();

when you get to the end of the month of say August, the next Sunday is 5 Sep and the first day of the week is 30 August, so diff is -25 and in the test:

for (let i = 0; i <= diff; i++) {

i is less than diff from the start so nothing is added to the array.

One fix is to get the number of days until the next Sunday and iterate for that many days, e.g.

// Return an array of dates from tomorrow until the 
// following Sunday.
function getDatesToSunday(date = new Date()) {
  // Copy date so don't affect original
  let d = new Date(+date);
  // Get the number of days until the next Sunday
  let count = 7 - d.getDay();
  // Create array of Dates
  let dates = [];
  while (count--) { 
    dates.push (new Date(d.setDate(d.getDate() + 1)));
  }
  return dates;
}

console.log('Given Sun 29 Aug 2021:');
getDatesToSunday(new Date(2021, 7, 29))
  .forEach(d => console.log(d.toDateString()));
  
console.log('Rest of this week, or next if today is Sunday:');
getDatesToSunday()
  .forEach(d => console.log(d.toDateString()));

If the supplied date is Saturday, the above returns an array of just Sunday. If the supplied date is Sunday, it returns an array of the following Monday to Sunday, etc.

If you want to get multiple weeks of dates, add a second parameter, say weeks that defaults to 1, then add (weeks - 1) * 7 to count before the while loop. Test weeks first to ensure it's 1 or greater (starting a decrementing while loop with a negative number is not a good idea).

Or you can just keep adding days until Sunday:

const getDatesToSunday = (date = new Date()) => {
  // Setup
  let year = date.getFullYear(),
      month = date.getMonth(),
      day = date.getDate(),
      dates = [];
  // Add dates from tomorrow until Sunday
  do {
    dates.push(new Date(year, month, ++day));
  } while (dates[dates.length - 1].getDay());
  return dates;
};

getDatesToSunday().forEach(d=>console.log(d.toDateString()));
RobG
  • 142,382
  • 31
  • 172
  • 209
0

Here's an example of increasing the date when moving to the next month:

let date = new Date(); // get current date
const month = date.getMonth(); // get current month
date.setMonth(month + 1); // increase month by 1
Zia
  • 506
  • 3
  • 20