CFLib.org – Common Function Library Project

countArbitraryDays(startdate, enddate [, exclude] [, includeStartDate])

Last updated December 5, 2006
Download UDF

author

Isaac Dealey                                      Isaac Dealey

Version: 1 | Requires: ColdFusion 5 | Library: DateLib

 
Rated 3 time(s). Average Rating: 5.0

Description:
Returns the number of days between a start and end date, excluding a specified list of days, i.e. the number of tuesdays and thursdays - this UDF relies on formula instead of brute force to calculate the days and will perform better than other WeekDays/BusinessDays methods which loop from the start date to end date

Return Values:
Returns a number.

Example:

<cfset BusinessDaysThisMonth = countArbitraryDays(CreateDate(year(now()),month(now()),1),CreateDate(year(now()),month(now()),daysinmonth(now())))>

Parameters:

Name Description Required
startdate Starting date. Yes
enddate Ending date. Yes
exclude Days of the week (as a number) to include. Defaults to 1,7 No
includeStartDate Boolean value to indicate if startdate is included in count. Defaults to true. No

Full UDF Source:

<cfscript>
/**
* Returns the number of specific days between a start and end date - i.e. weekdays or workdays.
*
* @param startdate      Starting date. (Required)
* @param enddate      Ending date. (Required)
* @param exclude      Days of the week (as a number) to include. Defaults to 1,7 (Optional)
* @param includeStartDate      Boolean value to indicate if startdate is included in count. Defaults to true. (Optional)
* @return Returns a number.
* @author Isaac Dealey (info@turnkey.to)
* @version 1, December 5, 2006
*/

function countArbitraryDays(startdate,enddate) {
    var exclude = "1,7"; var IncludeStartDate = true;
    var daysperweek = 0; var days = 0;
    var weekday = ArrayNew(1); var x = 0;
    var maxdays = DateDiff("d",dateadd("d",-1,startdate),enddate);
    
    switch (arrayLen(arguments)) {
        case 4: { IncludeStartDate = arguments[4]; }
        case 3: { exclude = arguments[3]; }
    }
    
    // create an array to hold days of the week with 1 or 0 indicating if the day is counted
    arraySet(weekday,1,7,1); exclude = listToArray(exclude);
    for (x = 1; x lte arrayLen(exclude); x = x + 1) { weekday[exclude[x]] = 0; } // set the value of any excluded day to 0
    daysperweek = arraySum(weekday); // count the number of included days in a full week
    days = daysperweek * int(maxdays/7); // get the number of included days in all full weeks
    for (x = 1; x lte maxdays mod 7; x = x + 1) { // add any remaining days in the last partial week
        days = days + weekday[dayofweek(enddate)];
        enddate = dateadd("d",-1,enddate);
    }
    
    // if excluding the start date, remove the value that might have been added for the starting day
    if (not includeStartDate) { days = days - weekday[dayofweek(startdate)]; }
    
    return days;
}
</cfscript>

Search CFLib.org


Latest Additions

Shawn Porter Shawn Porter added
DeMoronize
3 hour(s) ago

Chris Carey Chris Carey added
readPropertiesFi...
1 day(s) ago

Randy Johnson Randy Johnson added
lastDayofWeek
3 day(s) ago

Frank Marion Frank Marion added
sitemapPing
7 day(s) ago

Top Rated

James Sleeman                                     QuickSort
Rated 5.0, 3 time(s)

Barney Boisvert indentXml
Rated 5.0, 3 time(s)

Nathan Dintenfass                                 queryColumnsToSt...
Rated 5.0, 3 time(s)

Kevin Pepperman generateSsccAsn
Rated 5.0, 3 time(s)

Created by Raymond Camden / Design by Justin Johnson