The problem is:
Take a date object, set the time to 09:15 the following day. If that day is a Saturday, Sunday or Monday bump the date to the next Tuesday.
Solution:
const reminderTime = (date: Date): Date => {
const response = new Date(date);
const SUNDAY = 0;
const MONDAY = 1;
const TUESDAY = 2;
const SATURDAY = 6;
response.setDate(response.getDate() + 1);
if ([SUNDAY, MONDAY, SATURDAY].some((x) => x === response.getDay())) {
response.setDate(response.getDate() + ((TUESDAY + 7 - response.getDay()) % 7));
}
response.setHours(9);
response.setMinutes(15);
return response;
};
Is the date local or UTC? That depends on how the Date object passed into the method was created. In either case local or UTC is preserved.
Here are some proofs:
console.log(reminderTime(new Date(2020, 10, 2)));
// Returns Tue Nov 03 2020 09:15:00 GMT+0000 (Greenwich Mean Time)
console.log(reminderTime(new Date(2020, 10, 3)));
// Returns Wed Nov 04 2020 09:15:00 GMT+0000 (Greenwich Mean Time)
console.log(reminderTime(new Date(2020, 10, 4)));
// Returns Thu Nov 05 2020 09:15:00 GMT+0000 (Greenwich Mean Time)
console.log(reminderTime(new Date(2020, 10, 5)));
// Returns Fri Nov 06 2020 09:15:00 GMT+0000 (Greenwich Mean Time)
console.log(reminderTime(new Date(2020, 10, 6)));
// Returns Tue Nov 10 2020 09:15:00 GMT+0000 (Greenwich Mean Time)
console.log(reminderTime(new Date(2020, 10, 7)));
// Returns Tue Nov 10 2020 09:15:00 GMT+0000 (Greenwich Mean Time)
console.log(reminderTime(new Date(2020, 10, 8)));
// Returns Tue Nov 10 2020 09:15:00 GMT+0000 (Greenwich Mean Time)
console.log(reminderTime(new Date(2020, 10, 9)));
// Returns Tue Nov 10 2020 09:15:00 GMT+0000 (Greenwich Mean Time)
console.log(reminderTime(new Date(2020, 11, 31)));
// Returns Fri Jan 01 2021 09:15:00 GMT+0000 (Greenwich Mean Time)
console.log(reminderTime(new Date(2020, 1, 29)));
// Returns Tue Mar 03 2020 09:15:00 GMT+0000 (Greenwich Mean Time)
console.log(reminderTime(new Date(2020, 1, 30)));
// Returns Tue Mar 03 2020 09:15:00 GMT+0000 (Greenwich Mean Time)