How do I retrieve the dates for specific days of the week for all months in a year, and put them into an array?

While there is no ColdFusion specific function to return this, it is possible to build such functionality using ColdFusion's various date functions. Here is a UDF (user-defined function) as well as an example:

<cfscript>
function getEveryDOW(dowlist) {
	var year = year(now());
	var day1 = "";
	var x = "";
	var thisDOW = "";
	var result = arrayNew(1);
	var initialDOW = "";
	var offset = "";
	
	if(arrayLen(arguments) gte 2) year = arguments[2];
	day1 = createDate(year, 1,1);
	initialDOW = dayOfWeek(day1);
	
	while(year(day1) is year) {
		for(x=1; x lte listlen(dowlist); x=x+1) {
			thisDOW = listGetAt(dowlist, x);
			offset = thisDOW - initialDOW;
			dayToAdd = dateAdd("d", offset, day1 );
			arrayAppend(result, dayToAdd);
		}		
		day1 = dateAdd("ww", 1, day1);
	}
	return result;
}
</cfscript>
<cfset dowList = "1,3">
<cfset dArr = getEveryDow(dowlist)>
<cfdump var="#dArr#">

The UDF is a little bit complicated so let me explain it. The basic theory behind it is to start with a date object corresponding to the beginning of the year. Figure out what day of the week that is. Then we loop until the end of the year. We do this using dateAdd which lets us jump week by week. Since we knew the original day of the week, we can figure out the desired days of the week by figuring out an offset.

This question was written by Raymond Camden
It was last updated on September 29, 2006.

Categories

Dates/Times

Comments

comments powered by Disqus