This is useful if you need to set the start date on a day in the week following a specific date.
/** * params * date [JS Date()] * day_in_week [int] 1 (Mon) - 7 (Sun) */ function nextWeekdayDate(date, day_in_week) { var ret = new Date(date||new Date()); ret.setDate(ret.getDate() + (day_in_week - 1 - ret.getDay() + 7) % 7 + 1); return ret; } var date = new Date(); console.log(nextWeekdayDate(date, 5)); // Outputs the date next Friday after today. |
Be First to Comment