views:

80

answers:

3

I need to have a script on a page which dynamically changes the current week. I need to have the show "Week of February 15, 2010.

After next Sunday, I'd like the page to say "Week of February 22, 2010". I picked Monday for the display day but it could easily be Sunday.

Any suggestions are much appreciated!.

A: 
Adelf
+1  A: 

No jQuery inherently required.

var date = new Date();
var dayOfWeek = date.getDay();
date.setTime(date.getTime() - dayOfWeek*86400000); // 86 400 000 = # MS per day
alert('Week of ' + date.toLocaleFormat('%B %e, %Y') + '.'); // toLocaleFormat is a non-standard method

https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Date

Matt Ball
+1  A: 

jQuery is needed maybe just to easily show the result, but date functions are core javascript:

var date = new Date();
while (date.getDate() != 1 /* Monday */) {
    date.setDate(date.getDate() - 1);
}
var months = new Array("January", "February", "March", "April", 
                       "May", "June", "July", "August", "September", 
                        "October", "November", "December");

$("#some-id").val("Week of " + months[date.getMonth()] + " " +
                  date.getDate() + ", " + date.getFullYear());
Alex LE
I'd recommend using a `Date` format method rather than cobbling together your own - though I admit, the `toLocaleFormat` in my answer is non-ideal since it is non-standard.
Matt Ball