Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save trycf/9c86d7faca55849932a0dc708427b599 to your computer and use it in GitHub Desktop.
Save trycf/9c86d7faca55849932a0dc708427b599 to your computer and use it in GitHub Desktop.
TryCF Gist
<!---
# ColdFusion Challenge: Calculate Business Days
## Goal
Implement a function called `businessDaysBetween`
that calculates the number of working days between two dates,
excluding weekends and holidays.
## Function Specification
### Function Name
`businessDaysBetween`
### Parameters
1. **`startDate`** (Date) - The beginning date of the period (inclusive)
2. **`endDate`** (Date) - The ending date of the period (inclusive)
3. **`holidays`** (Array) - An array of specific dates to exclude as holidays
4. **`weekendDays`** (Array, Optional) - Days of the week considered weekends
- Uses ColdFusion's `dayOfWeek()` numbering system:
- 1 = Sunday, 2 = Monday, 3 = Tuesday, 4 = Wednesday, 5 = Thursday, 6 = Friday, 7 = Saturday
- Default value: `[1, 7]` (Sunday and Saturday)
### Return Value
- **Type**: Numeric
- **Description**: Total count of business days in the specified period
## Business Day Definition
A business day is any day that meets BOTH criteria:
- **NOT** a weekend day (not in the `weekendDays` array)
- **NOT** a holiday (not in the `holidays` array)
--->
<cffunction name="businessDaysBetween" returntype="numeric" access="public">
<cfargument name="startDate" type="date" required="true">
<cfargument name="endDate" type="date" required="true">
<cfargument name="holidays" type="array" required="true">
<cfargument name="weekendDays" type="array" default="#[1,7]#">
<cfset var businessDayCount = 0>
<cfscript>
totalDays = dateDiff("d", startDate, endDate);
holidays.each(function(item){
var compareStart = item.compare(startDate);
var compareEnd = item.compare(endDate);
if(compareStart >= 0 && compareEnd < 0) {
totalDays -= 1;
}
});
reachedEndDate = false;
counter = 0;
currentDate = startDate;
while(!reachedEndDate) {
dayOfWeekResult = dayOfWeek(currentDate);
weekendDays.each(function(item){
if( dayOfWeekResult == item ) {
totalDays -= 1;
}
});
currentDate = dateAdd('d', 1, currentDate);
if(currentDate.compare(endDate) == 0) {
reachedEndDate = true;
}
}
businessDayCount = totalDays;
</cfscript>
<cfreturn businessDayCount>
</cffunction>
<!--- Define UK Holidays for 2024 --->
<cfset ukHolidays = [
<!--- 2024 UK Bank Holidays --->
createDate(2024, 1, 1), <!--- New Year's Day --->
createDate(2024, 3, 29), <!--- Good Friday --->
createDate(2024, 4, 1), <!--- Easter Monday --->
createDate(2024, 5, 6), <!--- Early May Bank Holiday --->
createDate(2024, 5, 27), <!--- Spring Bank Holiday --->
createDate(2024, 8, 26), <!--- Summer Bank Holiday --->
createDate(2024, 12, 25), <!--- Christmas Day --->
createDate(2024, 12, 26) <!--- Boxing Day --->
]>
<cfset startDate = createDate(2024, 1, 1)>
<cfset endDate = createDate(2024, 12, 31)>
<!--- Call the function (once implemented) --->
<cfset businessDays = businessDaysBetween(startDate, endDate, ukHolidays)>
<cfoutput>
<h2>Business Days Calculation</h2>
<p>Start Date: #dateFormat(startDate, "yyyy-mm-dd")#</p>
<p>End Date: #dateFormat(endDate, "yyyy-mm-dd")#</p>
<p>Total Business Days: #businessDays#</p>
<h3>UK Holidays Excluded:</h3>
<ul>
<cfloop array="#ukHolidays#" index="holiday">
<li>#dateFormat(holiday, "yyyy-mm-dd")# - #getHolidayName(holiday)#</li>
</cfloop>
</ul>
</cfoutput>
<!--- Helper function to get holiday names (Provided) --->
<cffunction name="getHolidayName" returntype="string">
<cfargument name="holidayDate" type="date" required="true">
<cfset var dateStr = dateFormat(arguments.holidayDate, "yyyy-mm-dd")>
<cfset var holidayNames = {
"2024-01-01": "New Year's Day",
"2024-03-29": "Good Friday",
"2024-04-01": "Easter Monday",
"2024-05-06": "Early May Bank Holiday",
"2024-05-27": "Spring Bank Holiday",
"2024-08-26": "Summer Bank Holiday",
"2024-12-25": "Christmas Day",
"2024-12-26": "Boxing Day"
}>
<cfreturn structKeyExists(holidayNames, dateStr) ? holidayNames[dateStr] : "Unknown Holiday">
</cffunction>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment