countArbitraryDays(startdate, enddate [, exclude] [, includeStartDate])
Last updated December 5, 2006
Version: 1 | Requires: ColdFusion 5 | Library: DateLib
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:
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
Tayo Akinmade added
arrayTrim
3 day(s) ago
Will Belden added
longTime
9 day(s) ago
James Sleeman added
quickSort
19 day(s) ago
Ben Forta added
GetHostAddress
22 day(s) ago
Top Rated
EksporSQLData
Rated 5.0, 16 time(s)
backupDatabase
Rated 5.0, 13 time(s)
indentXml
Rated 5.0, 10 time(s)
generateSsccAsn
Rated 5.0, 4 time(s)