Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get date of specific day of the week in JavaScript

Tags:

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.

like image 774
Oscar Godson Avatar asked Sep 03 '10 19:09

Oscar Godson


2 Answers

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

like image 109
pepkin88 Avatar answered Oct 14 '22 18:10

pepkin88


Use the DateJS library.

Date.parse('next wednesday'); 
like image 34
Adam Avatar answered Oct 14 '22 19:10

Adam