Oscar Godson Oscar Godson -3 years ago 127
Javascript Question

Get date of specific day of the week in JavaScript

I think i'm mainly having a brain fart. I just want to fast forward a

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 (
) to return 09/08/10 (next wednesday).

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

P.S. jQuery is cool too.

Answer Source
function nextSession(date) {
    var ret = new Date(date||new Date());
    ret.setDate(ret.getDate() + (3 - 1 - ret.getDay() + 7) % 7 + 1);
    return ret;

This one will set date to next Wednesday. If it is already Wednesday, result will be one week later from given date.

EDIT: Added possibility of calling function without a parameter: nextSession() - it returns next session date from today

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download