var dayOfWeek = 5;//friday var date = new Date(2013, 10, 13); date.setDate(date.getDate() + (dayOfWeek + 7 - date.getDay()) % 7);
Here is what the above code is Doing:
1. Create a new date object for the 13th of November 2013.
2. Get the day of the week for that date (0 = Sunday, 1 = Monday, etc).
3. Add the number of days until the next Friday (5 – date.getDay()).
4. Add that number of days to the date.
5. Use the modulus operator to ensure that if the date is already a Friday, we don’t add another week.