While integrating a new feature for a client to flag customers that were requesting vouchers to be reissued, a business rule was required to determine whether the action should be performed during the current or next month. My client indicated that anything flagged during the last week of a month should be allocated towards the next month.
This wasn't a complex request, but there's no built-in function in ColdFusion for it. There's also no UDF available in CFLib.org DateLib... so here it is. Hopefully it will save someone some time and effort.
Without passing any options, it will use the current date & year. An option to include the weekend as part of the decision has been added.
Source Code
https://gist.github.com/JamoCA/cb8bd3f92d9b52ab97e376a6e7c32906
<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> |
Top comments (0)