CFLib.org – Common Function Library Project

getEveryDOW(dowList[, startdate][, enddate])

Last updated July 27, 2011

author

Raymond Camden

Version: 2 | Requires: CF5 | Library: DateLib

Description:
Returns every occasion of a day of the week. A list of days of the week can be used. The UDF returns an array of date objects corresponding to the days of the week requested.

Return Values:
Returns an array.

Example:

<cfset dowList = "1,3">
<cfset dArr = getEveryDow(dowlist)>
<cfdump var="#dArr#">

Parameters:

Name Description Required
dowList A list of days of the week in numeric form. Yes
startdate Initial range for search. Defaults to the beginning of the current year. No
enddate Initial range for search. Defaults to the end of the current year. No

Full UDF Source:

/**
 * Returns every occasion of a day of the week. A list of days of the week can be used.
 * Updates by Scott Pinkston and Peter Boughton to support a 'range' instead of just a year.
 * Fix by Jim O'Keefe to prevent values outside of this year.
 * 
 * @param dowList      A list of days of the week in numeric form. (Required)
 * @param startdate      Initial range for search. Defaults to the beginning of the current year. (Optional)
 * @param enddate      Initial range for search. Defaults to the end of the current year. (Optional)
 * @return Returns an array. 
 * @author Raymond Camden (ray@camdenfamily.com) 
 * @version 2, July 27, 2011 
 */
function getEveryDOW(dowlist) {
    var day1 = "";
    var x = "";
    var thisDOW = "";
    var result = arrayNew(1);
    var initialDOW = "";
    var offset = "";
    var dateToAdd = "";
    var startdate = createDate(year(now()), 1, 1);
    var enddate = createDate(year(now()), 12, 31);
    
    day1 = startdate;
    initialDOW = dayOfWeek(day1);

    if(arrayLen(arguments) gte 2) {
        startdate = arguments[2];
    }
    if(arrayLen(arguments) gte 3) {
        enddate = arguments[3];
    }

    while( day1 LT enddate ) {
        for(x=1; x lte listlen(dowlist); x=x+1) {
            thisDOW = listGetAt(dowlist, x);
            offset = thisDOW - initialDOW;
            dayToAdd = dateAdd("d", offset, day1 );
            if (dayToAdd gte startDate and dayToAdd lte endDate) {arrayAppend(result, dayToAdd);}
        }
        day1 = dateAdd("ww", 1, day1);
    }
    return result;
}

Search CFLib.org


Latest Additions

Raymond Camden added
QueryDeleteRows
November 04, 2017

Leigh added
nullPad
May 11, 2016

Raymond Camden added
stripHTML
May 10, 2016

Kevin Cotton added
date2ExcelDate
May 05, 2016

Raymond Camden added
CapFirst
April 25, 2016

Created by Raymond Camden / Design by Justin Johnson