Getting Dates For A Particular Day Of The Week In Upcoming X Weeks
A modification of a simple Javscript function used to get the dates for a particular day of the week in the upcoming X weeks.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | <script language="javascript"> <!--// var todaysDate = new Date(); var startDate = todaysDate, endDate = startDate; function getDayXOfWeekArray(dayXOfWeek,numWeeks) { var dayXOfWeekArray = new Array(); var d = new Date(); startDate = d; //Updating the endDate based on the number of weeks we want the dates for endDate.setTime(todaysDate.getTime() + (86400000*7*numWeeks)); endDateDay = endDate.getDate(); //Getting the dates var startDateTime = startDate.getTime(); var endDateTime = endDate.getTime(); var loopDate = new Date(); var dow, index=0; for (var i=startDateTime;i<=endDateTime;i+=86400000) { loopDate.setTime(i); dow = loopDate.getDay(); if (dow == dayXOfWeek) { //Storing the date //NOTE: I use formatDate() function from http://www.mattkruse.com/javascript/date/index.html dayXOfWeekArray[index] = formatDate(loopDate,"ddNNNyyyy"); index++; i+=86400000; } } return dayXOfWeekArray; }//end-of-getDayXOfWeekArray //--> </script> |
The dayXOfWeekArray returned by the getDayXOfWeekArray function stores the dates in the ddMMMYYYY format. For example, if we want the dates of the next 10 Fridays, the parameters passed to this function would look like
var dayXOfWeekArray = getDayXOfWeekArray(5,10);
The dayXOfWeekArray would contain 10 date strings (17Jul2009, 24Jul2009, 31Jul2009, 07Aug2009, 14Aug2009, 21Aug2009, 28Aug2009, 04Sep2009, 11Sep2009 & 18Sep2009) all of which fall between today (12Jul2009) and 20Sep2009.
Using the formatDate() function from http://www.mattkruse.com/javascript/date/index.html, it is very easy to format dates. Moreover, the required Javascript file, date.js is just 6 kb in size.