1

I am trying to calculate the dates of next week in a concise and efficient manner. So far, I am able to calculate next monday correctly and convert it to the required mm/dd/yyyy format. However, how could i do this for the entire week, while taking in to account month endings? I am concerned adding one to the date calculated 7 times will cause some silly errors in the future. Thanks.

Here is my code so far:

const getNextWeekDates = () => {
      const nextMonday = new Date();
      
      nextMonday.setDate(
        nextMonday.getDate() + ((1 + 7 - nextMonday.getDay()) % 7)
      );
      
    
      let dd = nextMonday.getDate();
      
      let mm = nextMonday.getMonth() + 1;
      
      const yyyy = nextMonday.getFullYear();
      
    
      if (dd < 10) dd = `0${dd}`;
      if (mm < 10) mm = `0${mm}`;
    
      return {
        monday: `${mm}/${dd}/${yyyy}`,
        tuesday: '',
        wednesday: '',
        thursday: '',
        friday: '',
        saturday: '',
        sunday: ''
      };
    };
    
    console.log(getNextWeekDates())
CodeBug
  • 1,649
  • 1
  • 8
  • 23
klaurtar1
  • 700
  • 2
  • 8
  • 29

0 Answers0