So, if you have your desired working day at DesiredWeekday, do this (jsfiddle here ):
var DesiredWeekday = 4; // this is Thursday var GivenDate = new Date(); alert(GivenDate.toString()); // This gives you the desired weekday but makes sure that it is at least two weeks after the given date var TwoWeeksAfterNextOccurrenceOfDesiredWeekday = new Date(); TwoWeeksAfterNextOccurrenceOfDesiredWeekday.setDate(GivenDate.getDate() + (DesiredWeekday - GivenDate.getDay() + 7) % 7 + 14); alert("two weeks after next thursday is = " + TwoWeeksAfterNextOccurrenceOfDesiredWeekday.toString()); // This gives you the desired weekday for the week after next week. var DesiredWeekdayOfWeekAfterNextWeek = new Date(); DesiredWeekdayOfWeekAfterNextWeek.setDate(GivenDate.getDate() + DesiredWeekday - GivenDate.getDay() + 14); alert("Thursday of Week after next is = " + DesiredWeekdayOfWeekAfterNextWeek.toString());
From your recent comments, it seems that you want this to be the first of these two methods. Let me explain the theory of this first method, as it can be used for things like monthly repetition, when you want to calculate something like the first Friday of the month or the third Tuesday, etc.
The idea in your case is that you want two weeks from the next Thursday after your date (or input). So, firstly, we have to move on to the next Thursday. Suppose this date is Tuesday. To move on to the next Thursday, we need to add two days, that is, the difference between the day of the week on Thursday (4), and you indicated the date of the day of the week (2 on Tuesday). So simple, we simply add to our date the difference between the desired weekday and the day of the week. So, the formula that we still have is:
Calculated Date = Given Date + (Desired weekday - Given Date weekday)
But if our date is after the desired working day, we have a problem. Let's say that our date is Friday, and we want to move on to the next Thursday, if we add this difference, the difference will be: the desired weekday (Thursday: 4) - the specified date of the day of the week (Friday: 5). And this is -1, and we donβt want to go back to the previous Thursday, we want to go to the next Thursday, so we need to add a week. This brings us to:
Calculated Date = Given Date + (Desired weekday - Given Date weekday + 7)
But back to our case, when the specified date is the day of the week until the desired working day. If our given date is Tuesday, and we add seven, as in this formula, we are going to add 9 days and go on the next week Thursday, and not next Thursday. So, how can we prevent the addition of 7 if the total is greater than 7? Or, how do we take it out 7 if the total is more than 7? The answer is the module operator:
Calculated Date = Given Date + ((Desired weekday - Given Date weekday + 7) % 7)
This will give you the first Thursday after your date. Now, if you want two weeks after this, you will add 14, for example:
Calculated Date = Given Date + ((Desired weekday - Given Date weekday + 7) % 7) + 14
Or in javascript
CalculatedDate = GivenDate.setDate(GivenDate.getDate() + (DesiredWeekday - GivenDate.getDay() + 7) % 7 + 14);