CFLib.org – Common Function Library Project

getEveryNthDay(dow, nth, yy)

Last updated March 30, 2006

author

charlie griefer

Version: 1 | Requires: CF5 | Library: DateLib

Description:
returns an array of dates. dates are every nth day of week (e.g. every 2nd tuesday) for a given year.

Return Values:
Returns an array.

Example:

3rd friday of each month (friday = 6):
<br /><br />

<cfdump var="#getEveryNthDay(6, 3, 2006)#">

Parameters:

Name Description Required
dow The numeric day of the week. Yes
nth Week number in the month. Yes
yy Year to iterate over. Yes

Full UDF Source:

/**
 * determine every nth day of week for a given year.
 * 
 * @param dow      The numeric day of the week. (Required)
 * @param nth      Week number in the month. (Required)
 * @param yy      Year to iterate over. (Required)
 * @return Returns an array. 
 * @author charlie griefer (charlie@griefer.com) 
 * @version 1, March 30, 2006 
 */
function getEveryNthDay(dow,nth,yy) {
    var containerArray = arrayNew(1);
    
    var mm             = "";
    var dd             = "";
    var startDate    = "";
    var dateFound    = 0;
    
    if (val(dow) LT 1 OR val(dow) GT 7) return false;
    
    for (mm=1; mm LTE 12; mm=mm+1) {
        dateFound = 0;
        for (dd=1; dd LTE daysInMonth(createDate(yy, mm, 1)); dd=dd+1) {
            startDate = createDate(yy, mm, dd);
            if (dayOfWeek(startDate) EQ dow) {
                dateFound = dateFound + 1;
                if (dateFound EQ nth) arrayAppend(containerArray, startDate);
            }
        }
    }
    
    return containerArray;
}

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