Calculating a two-week forward form per day using marker day

I am trying to encode a Thursday date, which falls out two weeks after a certain date. This is more complicated than just adding 14 days to a specified date.

This is due to the fact that if the first Wednesday after the given date of the week is the start date, two weeks after it, counting on Thursdays, more than 14 days, when the Thursday of the week in which the date was announced is skipped.

I think it should be a JavaScript function that uses

// the variable will contain the current date var today = new Date ();

// the variable will contain the day number (for example, environment = 3). var thisDay = today.getDay ();

and then I put it in the this this this variable in the if statement.

To be clear, I am trying to use this code so that the start date is variable. Then I count in 2 weeks, skipping the week for which the date was announced.

+5
source share
5 answers

using moment.js you can do it like this:

moment().add(2, 'weeks').startOf('isoweek').add(3, 'days'); 

demo http://jsfiddle.net/n20vncp0/

+4
source

You can add 14 days, then add the difference between Thursday (4) and the current day of the week. Here is the fiddle .

 var date = new Date(); date.setDate(date.getDate() + 14 + (4 - date.getDay() + 7) % 7); 
+1
source

I think this is what you want. I have a function that calculates how many weeks ahead you want, and then the target day of the week, which it will find after the specified number of weeks.

I am using moment.js.

Check out JSfiddle here: https://jsfiddle.net/Ravvy/ea1u903k/1/

 $(document).ready(function () { var dateOfNextTwoThursdays = getDateAfterWeeks(2, 'Thursday'); $('#dateOfNextTwoThursdays').text(dateOfNextTwoThursdays); function getDateAfterWeeks(numberOfWeeks, dayString) { if (!numberOfWeeks || numberOfWeeks < 1 || !dayString) return null; if (dayString != 'Monday' && dayString != 'Tuesday' && dayString != 'Wednesday' && dayString != 'Thursday' && dayString != 'Friday' && dayString != 'Saturday' && dayString != 'Sunday') return null; var weeksFromNow = moment().add('days', numberOfWeeks * 7); var dayOfTheWeek = weeksFromNow.format('dddd'); if (dayOfTheWeek === dayString) return weeksFromNow; else { while (dayOfTheWeek != dayString) { weeksFromNow = weeksFromNow.add('days', 1); if (weeksFromNow.format('dddd') === dayString) return weeksFromNow; dayOfTheWeek = weeksFromNow.format('dddd'); } } } }); 
0
source

Why not just jump over the next week and then scroll through the days (adding them each time) until you land on Thursday?

 function getNextThursday(current){ if(!current){ var current = new Date(); } var newday = new Date(current.getTime() + (86400000 * 8)); // next week plus 1 day in ms while(newday.getDay() != 4){ newday = new Date(newday.getTime() + 86400000); } return newday; } var nextThursday = getNextThursday(); 
0
source

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); 
-1
source

Source: https://habr.com/ru/post/1236256/


All Articles