CFLib.org – Common Function Library Project

getWeekEnding(theMonth, theYear)

Last updated January 13, 2004

author

Brian Rinaldi

Version: 1 | Requires: CF5 | Library: DateLib

Description:
Returns an array of dates with the week ending date of each week in the month. The code defaults to friday as the week ending as this would typically be useful when creating some sort of business report.

Return Values:
Returns an array.

Example:

dump of week ending dates for the current month/year:

<cfdump var="#getWeekEnding(month(now()),year(now()))#">

Parameters:

Name Description Required
theMonth Month to use. Yes
theYear Year to use. Yes

Full UDF Source:

/**
 * Returns an array of dates with the week ending date of each week in the month.
 * 
 * @param theMonth      Month to use. (Required)
 * @param theYear      Year to use. (Required)
 * @return Returns an array. 
 * @author Brian Rinaldi (brinaldi@criticaldigital.com) 
 * @version 1, January 13, 2004 
 */
function getWeekEnding(theMonth,theYear) {
    /**
     * week ending day is a friday for our purposes as the end of the business week
     * this can be modified to return a week ending on whatever day you want
    */
    var endOfWeek = 6;
    var theDay = 0;
    var i = 1;
    var arrDate = arrayNew(1);
    
    var theDate = "";
    
    // loop to find the first friday of the month
    do {
        theDay = theDay + 1;
    } while (dayOfWeek(createDate(theYear,theMonth,theDay)) NEQ endOfWeek);
    // establish the first friday of the month
    theDate = createDate(theYear,theMonth,theDay);
    // set the first week end date in the array
    arrDate[i] = theDate;
    /**
     * loop through the rest of the month adding seven to the date until the date
     * exceeds the end of the month
    */
    i=i+1;
    while (month(dateAdd('d',7,theDate)) EQ theMonth) {
        theDate = dateAdd('d',7,theDate);
        arrDate[i] = theDate;
        i = i + 1;
    }
    return arrDate;
}

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