skip to Main Content

I’m getting in api response array of name of days e.g. [‘TUE’] and I need to get date of this value in next week and in format ‘YYYY-mm-dd’. Can you help me please?

reponse = ['TUE'];
const week = ['SUN','MON','TUE','WED','THU','FRI','SAT'];
 
if (week.includes(api.response[0])) {
   const date = new Date(new Date().setDate(new Date().getDate() + week.indexOf(response[0])) + 6);
   return date.toISOString().split('T')[0];
}
return '';

3

Answers


  1. Get the index from the week array, then use that to add days.

    function getNextDateByDay(day) {
      //
      const week = ['SUN', 'MON', 'TUE', 'WED', 'THU', 'FRI', 'SAT'];
    
      // Calculate the index of the next occurrence of the specified day of the week
      const index = (week.indexOf(day) + 7 - new Date().getDay()) % 7;
    
      // Create a new Date object for the next occurrence of the specified day of the week
      const now = new Date();
    
      // Set the date of the now object to the next occurrence of the specified day of the week
      now.setDate(now.getDate() + index);
    
      // Return the date of the next occurrence of the specified day of the week in YYYY-MM-DD format
      return now.toISOString().slice(0, 10);
    }
    
    // 2023-03-21
    console.log(getNextDateByDay('TUE'));
    Login or Signup to reply.
  2. You can use a general function for getting a particular day of the week, then just add 7 days to get the day in the next week (or add 7 days before getting the day in the week).

    Different cultures have different days for the start of the week, so a general function should allow the start of the week to be specified.

    The following function gets a particular day of the week given:

    1. The day to get as ECMAScript day number (0 = Sun, 1 = Mon, etc.)
    2. A Date that is in the required week (default is current date)
    3. The day the week starts on (ECMAScript day number, default is 0)
    /* Return day in week of date
     * @param {number} day - ECMAScript day number
     *                 0 = Sun, 1 = Mon, etc.
     * @param {Date} date - date in week to get day of
     * @param {number} weekStart - day that week starts on
     *                 ECMAScript day number as for day
     * @returns {Date}
     */
    function getDayInWeek(day, date = new Date(), weekStart = 0) {
      let d = new Date(date);
      // Move d to start of week - same day or earlier
      d.setDate(d.getDate() - d.getDay() + weekStart);
      // Move d to required day
      d.setDate(d.getDate() + ((day + 7 - weekStart) % 7));
    
      return d;
    }
    
    // Test date Sat 18 Mar 2023
    let d = new Date(2023, 2, 18);
    [
      // Thu of week starting on Sat: 23rd
      [4, d, 6],
      // Fri of week starting on Sun: 17th
      [5, d, 0],
      // Sat of week starting on Mon: 18th
      [6, d, 1],
      // Mon of week starting on Mon: 13th
      [1, d, 1],  
    ].forEach(([day, date, weekStart]) => console.log(
      getDayInWeek(day, date, weekStart).toDateString())
    );
    Login or Signup to reply.
  3. const response = ['TUE'];
    const week = ['SUN', 'MON', 'TUE', 'WED', 'THU', 'FRI', 'SAT'];
    
    if (week.includes(response[0])) {
      const today = new Date();
      const daysUntilNextWeekDay = (week.indexOf(response[0]) + 7 - today.getDay()) % 7;
      const date = new Date(today.setDate(today.getDate() + daysUntilNextWeekDay));
      const formattedDate = date.toISOString().split('T')[0];
      return formattedDate;
    }
    return '';
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search