Skip to content

Instantly share code, notes, and snippets.

@JamoCA
Last active November 7, 2024 17:57
Show Gist options
  • Save JamoCA/cb8bd3f92d9b52ab97e376a6e7c32906 to your computer and use it in GitHub Desktop.
Save JamoCA/cb8bd3f92d9b52ab97e376a6e7c32906 to your computer and use it in GitHub Desktop.
getLastWorkWeekInMonth ColdFusion UDF - Returns the date for the start of the last full work week (M-F) in a given month. #CFML
<cfscript>
/**
* getLastWorkWeekInMonth: Returns the date for the start of the last full work week (M-F) in a given month.
*
* @param Month A number representing the month (default = current month)
* @param Year A number representing the year (default = current year)
* @param includeWeekend A boolean flag to indicate whether to require the weekend as part of the decision
* @return Returns a date representing the start of the week.
* @author James Moberg http://sunstarmedia.com, @sunstarmedia
* @version 1, November 7, 2024
* @gist https://gist.github.com/JamoCA/cb8bd3f92d9b52ab97e376a6e7c32906
* @blog https://dev.to/gamesover/getlastworkweekinmonth-coldfusion-returns-last-full-work-week-23j3
* @twitter https://x.com/gamesover/status/1854582375231218034
* @LinkedIn https://www.linkedin.com/posts/jamesmoberg_getlastworkweekinmonth-coldfusion-returns-activity-7260349631472009216--VCH
*/
date function getLastWorkWeekInMonth(numeric month=month(now()), numeric year=year(now()), boolean includeWeekend=true) {
local.weekStartDay = 2; // monday
local.daysInMonth = daysinmonth(createdate(arguments.year, arguments.month, 1));
local.dayOfWeekOfLastDay = dayofweek(createdate(arguments.year, arguments.month, local.daysInMonth));
local.daysDifference = local.dayOfWeekOfLastDay - local.weekStartDay;
if(local.daysDifference lt 0){
local.daysDifference += 7;
}
local.date = createdate(arguments.year, arguments.month, local.daysInMonth - local.daysDifference);
local.endDay = (arguments.includeWeekend) ? 6 : 4;
if (month(dateadd("d", local.endDay, local.date)) neq arguments.month){
local.date = dateadd("ww", -1, local.date);
}
return local.date;
}
</cfscript>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment