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
Get the index from the week array, then use that to add days.
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: