DEV Community

Cover image for getLastWorkWeekInMonth() ColdFusion - Returns Last Full Work Week
James Moberg
James Moberg

Posted on

1

getLastWorkWeekInMonth() ColdFusion - Returns Last Full Work Week

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>
šŸ‘‹ While you are here

Reinvent your career. Join DEV.

It takes one minute and is worth it for your career.

Get started

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

šŸ‘‹ Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay