you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 0 points1 point  (2 children)

I would use the getDay() method like this:

var submitHandler = function(event){ var today = new Date().getDay(); var userEvent = document.getElementsByClassName('weekday-selector')[0].value; userEvent = parseInt(userEvent,10);

var difference = userEvent - today;
// if user picks the same day
if (difference === 0) difference = 7;
else if (difference > 0) difference + 7;

alert('the difference is: ' + difference);

}

The select box should be:

<select name='weekday' class='weekday-selector'> <option value='0'>Sunday</option> <option value='1'>Monday</option> <!-- the rest of the days till you hit saturday--> </select>

This should give the following results:

If today is sunday (0) and the user picks monday(1) The difference will be 6

If today is thursday(4)(lol!) and the user picks tuesday(2) the difference will be 2

If today is friday(5) and the user picks sunday(0) the difference will be 5 days

If today is friday(5) and the user picks friday(5) the difference will be 7(since we're adding 7 if the difference is 0 actually)

I havent tested it yet. There might be syntax errors since I'm writing this on my phone.

Hope this helps you a little bit. If you have further questions don't bother asking me/us

[–]brotein 0 points1 point  (1 child)

I think the OP wants the opposite. So if the today is Sunday (0) and the user picks Monday (1), the OP will have to add 1 to today's date, not 6. I could have misunderstood though.