CFLib.org – Common Function Library Project

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

Last updated December 5, 2006

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:

view plain print about
<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:

view plain print about
<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>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Tayo Akinmade Tayo Akinmade added
arrayTrim
3 day(s) ago

Will Belden Will Belden added
longTime
9 day(s) ago

James Sleeman James Sleeman added
quickSort
19 day(s) ago

Ben Forta Ben Forta added
GetHostAddress
22 day(s) ago

Top Rated

Darwan Leonardo Sitepu EksporSQLData
Rated 5.0, 16 time(s)

Darwan Leonardo Sitepu backupDatabase
Rated 5.0, 13 time(s)

Barney Boisvert indentXml
Rated 5.0, 10 time(s)

Kevin Pepperman generateSsccAsn
Rated 5.0, 4 time(s)

Created by Raymond Camden / Design by Justin Johnson