I was working on a project, and I needed to write a new function which calculates yesterday’s date and if yesterday was a Sunday, the function needed to return last Friday’s date. The first part of my function checks to see if the date supplied to the function is at least Tuesday. If the date passes that check, then we subtract one from the date and return the date.
If the date failed the first check, then we subtract 7 from the date to get a day last week, and then subtract the day of the week from that day in order to get to the beginning of the week. Finally we add 6 to that day in order to get Friday.
[cffunction name="getLastWorkDay" returntype="numeric" access="private"]
[cfargument required="true" type="date" name="DateToProcess"]
[cfif DayOfWeek(fix(arguments.DateToProcess)) GT 2 ]
[cfreturn CreateDate(Year(arguments.DateToProcess),Month(arguments.DateToProcess),Day(arguments.DateToProcess-1)) /]
[cfelse]
[cfreturn DateFormat((Fix(arguments.DateToProcess)-7) - DayOfWeek(Fix(arguments.DateToProcess)-7)+6)/]
[/cfif]
[/cffunction]
[cfoutput]Date: #DateFormat(getLastWorkDay(now()), "dddd, mmmm d, yyyy")#[/cfoutput]