Get date of specific day of the week in JavaScript

Viewed 23623

I think i'm mainly having a brain fart. I just want to fast forward a Date() until a specific day of the week and then get the Month, Day, Year for that.

E.g. today is 09/03/10 but i want a function (nextSession()) to return 09/08/10 (next wednesday).

How would I do this? The best thing I can think of is add a day to setDate() until getDay() == 3, but it's sorta ugly...

P.S. jQuery is cool too.

7 Answers

simple calculation:

//for next sunday, set 0, for saturday 6; wednesday == 3;
var needDay = 3;
var fromDate = new Date(); //or any date you calculate from
fromDate.setDate(fromDate.getDate() + (7 - fromDate.getDay()) + needDay);

console.log(fromDate);

+ (7 - fromDate.getDay()): adds to Date amount of days to change the date to sunday.
+ needDay: adds your day you need, if wednesday, just +3;

Related