0

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 '';
  • _"Can you help me please?"_ You will need to explain to us what is not working first. – phuzi Mar 17 '23 at 16:55
  • You can't find date with just day name. Can you be more specific with the response of the API? – Abdul Raheem Dumrai Mar 17 '23 at 16:55
  • 1
    So if today is Friday 2023-03-17, what do you expect for the answer `SAT`, `SUN`, `MON` and `FRI` respectively? What do you consider as first day of a week, so what is "next week" on a Sunday or Monday? – derpirscher Mar 17 '23 at 16:56
  • 1
    @AbdulRaheemDumrai You can if you precisely define what "next week" is in various border cases ... – derpirscher Mar 17 '23 at 17:00

3 Answers3

0

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())
);
RobG
  • 142,382
  • 31
  • 172
  • 209
  • How can it be that if "today" (that's what I assume `d` should be) is the 18th of March, a date in the "next week" gives the 13th of March? – derpirscher Mar 18 '23 at 10:29
  • @derpirscher—read the first sentence of the answer. This function gives the required day in the week of the supplied *date* and start of week day. If you want to add or subtract weeks, do that to the supplied date beforehand or to the returned date afterward. It's trivial to [add multiples of 7 days](https://stackoverflow.com/questions/563406/how-to-add-days-to-date) to add however many weeks you want. – RobG Mar 19 '23 at 22:57
-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'));
Lawrence Cherone
  • 46,049
  • 7
  • 62
  • 106
  • 1
    if `getNextDateByDay` is run with the same dayname as today (for instance `getNextDateByDay('FRI')` on a Friday) it will return today's date, instead of next week's date ... Because `week.indexOf(day)` will be equal to `new Date().getDay()` and thus `const index` will be `7 % 7 == 0` ... – derpirscher Mar 17 '23 at 21:52
  • 1
    Furthermore using `toISOString()` to get a formated datestring of a `Date` object is always dangerous, because `toISOString()` will always format the date and time in UTC. Imagine running this code at 3AM in a timezone that is UTC+5. Then your date will be one day off (ie it will for instance return the 2023-03-20 instead of 2023-03-21). Same, when this code is run an 10PM in a timezone that is UTC-5. It will return 2023-03-22 instead of 2023-03-21 ... – derpirscher Mar 17 '23 at 22:05
-1
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 '';

  • How does your answer differ from the [already existing one](https://stackoverflow.com/a/75770413/3776927), except that you don't even care to explain it? And as it doesn't differ at all, it also suffers from the same shortcomings ... – derpirscher Mar 18 '23 at 10:43
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 23 '23 at 06:45