3

How would I write this expression in JavaScript?

It is to represent a date that is 2 weeks, counted by each passing Thursday, but excludes the thursday of the week the date was made.

NeededDay = Today + (18 - DayOfWeek(today))

or since it is Wednesday, it could be written?

var date = new Date();
var NeededDate = date.getDay() + (18-3);

or

I wrote this but I do not know if it is right?

var value = 3; 
var GivenDate = value; 
var GivenDay = value.getDay(); 

var daysToSecondThursday = Givenday2.setDate(GivenDay + Givenday2.setDate(18 - GivenDay)); 

alert("two weeks after next thursday is = " + daysToSecondThursday.val());  

what is the correct way? ?

Karl Gjertsen
  • 4,690
  • 8
  • 41
  • 64
joe 222
  • 91
  • 7
  • Assuming I understand you correctly, your code translates to the following in JS: var today = new Date(); var newDate = new Date(today.getYear(), today.getMonth(), today.getDate() + 18 - today.getDay()); – ryanlutgen Nov 19 '15 at 03:19
  • I wrote this but I do not knnow if it is right? var value = 3; var GivenDate = value; var GivenDay = value.getDay(); var daysToSecondThursday = Givenday2.setDate(GivenDay + Givenday2.setDate(18 - GivenDay)); alert("two weeks after next thursday is = " + daysToSecondThursday.val()); – joe 222 Nov 19 '15 at 08:08
  • If `value = 3`, what do you expect `value.getDay()` to return? `value` is an integer, it has no `getDay()` method. Furthermore, where does the `Givenday2` come from? What do you expect `daysToSecondThrusday.val()` to return? – ROAL Nov 19 '15 at 08:25
  • 3 is to represent a date beghow to make javascript runing feed in – joe 222 Nov 19 '15 at 08:28
  • the 3 to represent new Date() value for Wednesday... meaning that the 3 is a value coming in.. – joe 222 Nov 19 '15 at 08:39

1 Answers1

1

You could use:

function GetThursdayIn2Weeks(date)
{

    var day = date.getDay();

    // Add 2 weeks.
    var newDate = new Date(date.setTime(date.getTime() + (14 * 86400000)));

    // Adjust for Thursday.
    var adjust = 4 - day;
    if (adjust <= 0) // Might need to be changed - See comments!
      adjust +=7;

    // Apply Thursday adjustment.
    newDate = new Date(newDate.setTime(newDate.getTime() + (adjust * 86400000)));

    return newDate;

}

If the date passed in is Thursday, then it will return two weeks from the following Thursday. If this is not what you want, then adjust the if (adjust <= 0) code above to be:

if (adjust < 0)

Here is a jsFiddle: http://jsfiddle.net/kgjertsen/ec7vnezn/

Karl Gjertsen
  • 4,690
  • 8
  • 41
  • 64
  • Can you explain the "(14 * 86400000))"?.. it is 14 days times... ? – joe 222 Nov 19 '15 at 09:55
  • 86,400,000 is the number of milliseconds in a day. `setDate()` doesn;t like adding days to a date, so in the example code I use `setTime()`, which works better. see: http://stackoverflow.com/a/6963328/2394816 – Karl Gjertsen Nov 19 '15 at 09:59
  • Regarding the 'adjust', and when testing it, by using a Monday (i.e 1) then i will get 25 days in the end but it should be 18. any ideas? is a switch case function using jquary a safe approach? – joe 222 Nov 19 '15 at 10:26
  • Are you sure? The code would add 14 days, then 4-1=3, so would add 3 days, which would add up to 17, which is correct. I have added a fiddle: http://jsfiddle.net/kgjertsen/ec7vnezn, which shows Monday November 17th, going to Thursday December 3rd. – Karl Gjertsen Nov 19 '15 at 11:39
  • why do you have "date1.setDate(16);" where does the 16 come in? – joe 222 Nov 19 '15 at 11:56
  • Sets the day to be the 16th, which is a Monday This is shown in the alert box. The result that comes back is correct at 17 days, not 25. – Karl Gjertsen Nov 19 '15 at 11:57
  • on a side note, how would i make this set off a different function every day that passed, remind me about the due date cumming up, but before the end of the date the process is due? – joe 222 Nov 19 '15 at 12:22
  • Not sure what you mean. Sounds like another question. :-) – Karl Gjertsen Nov 19 '15 at 12:25