Ask Ben: Finding The Last Monday Of The Month In ColdFusion

Posted July 31, 2007 at 9:22 AM

Tags: ColdFusion, Ask Ben

I see that you have been doing a lot of calendar stuff lately and I have question. How can I find the last monday of the month? TIA!

Finding the last monday of the month, or last "whatever" day of the week, just takes a little bit of math. Basically, what you want to do is get the last week of the month. This we can easily do by finding the last day of the month (first day of next month minus one day). From the last day, we can get the last week. Then, from the last week we can get the day of the week we are looking for. Of course, there is nothing about the day, at that point, that requires it to be in the current month. Therefore, all we need to do is compare the month of that day to the month we are looking at. If they are the same, we have our "last day"; if they are different, then by subtracting a week from that day, we will have found our "last day".

That's a bit of a mouthful; it might just be easier to walk through the code:

 Launch code in new window » Download code as text file »

  • <!--- Get the current month based on the curren date. --->
  • <cfset dtThisMonth = CreateDate(
  • Year( Now() ),
  • Month( Now() ),
  • 1
  • ) />
  •  
  • <!---
  • Now, get the last day of the current month. We
  • can get this by subtracting 1 from the first day
  • of the next month.
  • --->
  • <cfset dtLastDay = (
  • DateAdd( "m", 1, dtThisMonth ) -
  • 1
  • ) />
  •  
  • <!---
  • Now, the last day of the month is part of the last
  • week of the month. However, there is no guarantee
  • that the "Monday" of this week will be in the current
  • month. If Sunday was the last day of the month, then
  • Monday would be the first day of the next month.
  • Regardless, let's get the date of the target day.
  • --->
  • <cfset dtMonday = (
  • dtLastDay -
  • DayOfWeek( dtLastDay ) +
  •  
  • <!---
  • This is the day of the week that we are
  • trying to find (2 = Monday).
  • --->
  • 2
  • ) />
  •  
  •  
  • <!---
  • Now, we have a Monday, but we are not exactly sure if
  • this Monday is in the current month. if is not, then
  • we know it is the first Monday of the next month, in
  • which case, subracting 7 days (one week) from it will
  • give us the last Monday of the current Month.
  • --->
  • <cfif (Month( dtMonday ) NEQ Month( dtThisMonth ))>
  •  
  • <!--- Subract a week. --->
  • <cfset dtMonday = (dtMonday - 7) />
  •  
  • </cfif>
  •  
  •  
  • <!---
  • Output the date of the last Monday. CAUTION: Our date
  • math above has left us with a NUMERIC DATE, not a
  • traditional date/time stamp. Therefore, be careful
  • about using things like IsDate() as this numeric date
  • will NOT pass these checks.
  • --->
  • <p>
  • Last Monday Of Month:
  • #DateFormat( dtMonday, "full" )#
  • </p>

Running the above code we get the following output:

Last Monday Of Month: Monday, July 30, 2007

Now, we just found the last Monday, but we could easily abstract this out to find any "last day" of the month. Notice that in the above algorithm, we only referred to Monday as being day "2" in one place. By replacing that "2" with a variable, suddenly, we can find any last day of the week - of the month.

Let's wrap that up in a ColdFusion user defined function, GetLastDayOfWeekOfMonth(). I know that function name is a mouthful, but frankly, I couldn't come up with anything better to call it:

 Launch code in new window » Download code as text file »

  • <cffunction
  • name="GetLastDayOfWeekOfMonth"
  • access="public"
  • returntype="date"
  • output="false"
  • hint="Returns the date of the last given weekday of the given month.">
  •  
  • <!--- Define arguments. --->
  • <cfargument
  • name="Date"
  • type="date"
  • required="true"
  • hint="Any date in the given month we are going to be looking at."
  • />
  •  
  • <cfargument
  • name="DayOfWeek"
  • type="numeric"
  • required="true"
  • hint="The day of the week of which we want to find the last monthly occurence."
  • />
  •  
  • <!--- Define the local scope. --->
  • <cfset var LOCAL = StructNew() />
  •  
  • <!--- Get the current month based on the given date. --->
  • <cfset LOCAL.ThisMonth = CreateDate(
  • Year( ARGUMENTS.Date ),
  • Month( ARGUMENTS.Date ),
  • 1
  • ) />
  •  
  • <!---
  • Now, get the last day of the current month. We
  • can get this by subtracting 1 from the first day
  • of the next month.
  • --->
  • <cfset LOCAL.LastDay = (
  • DateAdd( "m", 1, LOCAL.ThisMonth ) -
  • 1
  • ) />
  •  
  • <!---
  • Now, the last day of the month is part of the last
  • week of the month. However, there is no guarantee
  • that the target day of this week will be in the current
  • month. Regardless, let's get the date of the target day
  • so that at least we have something to work with.
  • --->
  • <cfset LOCAL.Day = (
  • LOCAL.LastDay -
  • DayOfWeek( LOCAL.LastDay ) +
  • ARGUMENTS.DayOfWeek
  • ) />
  •  
  •  
  • <!---
  • Now, we have the target date, but we are not exactly
  • sure if the target date is in the current month. if
  • is not, then we know it is the first of that type of
  • the next month, in which case, subracting 7 days (one
  • week) from it will give us the last occurence of it in
  • the current Month.
  • --->
  • <cfif (Month( LOCAL.Day ) NEQ Month( LOCAL.ThisMonth ))>
  •  
  • <!--- Subract a week. --->
  • <cfset LOCAL.Day = (LOCAL.Day - 7) />
  •  
  • </cfif>
  •  
  •  
  • <!--- Return the given day. --->
  • <cfreturn DateFormat( LOCAL.Day ) />
  • </cffunction>

Now that we have that in place, let's run a little script to test it out across the year:

 Launch code in new window » Download code as text file »

  • <p>
  • <!--- Loop over every month in year. --->
  • <cfloop
  • index="intMonth"
  • from="1"
  • to="12"
  • step="1">
  •  
  • <!--- Get last monday. --->
  • #GetLastDayOfWeekOfMonth(
  • "#intMonth#/1/2007",
  • 2
  • )#<br />
  •  
  • </cfloop>
  • </p>

Running that code, we get the following output:

29-Jan-07
26-Feb-07
26-Mar-07
30-Apr-07
28-May-07
25-Jun-07
30-Jul-07
27-Aug-07
24-Sep-07
29-Oct-07
26-Nov-07
31-Dec-07

Notice that our July 30, 2007 lines up with our first example. And, checking my calendar, the rest of the dates line up nicely.

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Other Searches  |  Print Page




Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

Reader Comments

Jul 31, 2007 at 10:26 AM // reply »
18 Comments

The problem with the numeric date due to your date math, could you get round this by just using DateAdd functions instead of doing arithmetic directly on the date object?


Jul 31, 2007 at 11:29 AM // reply »
7,539 Comments

@Duncan,

Yes, DateAdd() would get around the formatting issue, however, I am pretty sure that doing date math directly is faster than calling the DateAdd(). Plus, and this is just my personal preference, doing math looks nicer. DateAdd(), to me, adds a lot of "noise" to the code that really isn't adding any benefit (of course, that is only true if you know that you can do date math).... but again, mostly personal preference.


Aug 2, 2007 at 1:36 PM // reply »
3 Comments

i thought of another interesting way to accomplish the same thing, which was a little more intuitive for me. instead of just subtracting one week if the day that you originally selected happens to fall in the next month, you simply loop "backwards" from the last day of the month by subtracting zero through six from it until you find the day you are looking for. if you were looking for the last sunday, and sunday happens to be the last day of the month, you're golden. otherwise you just work backwards saturday, friday, etc. until you hit your target day.


Aug 5, 2007 at 5:16 PM // reply »
7,539 Comments

@Sherrardb,

Yes, that would accomplish the same thing, so if you find that method more intuitive, by all means go for it.


Aug 7, 2007 at 12:39 AM // reply »
2 Comments

Thanks Ben, Great solution. You saved me from having to think too hard ;)


May 15, 2009 at 11:20 AM // reply »
3 Comments

Thanks a lot, this last day of the month methodology helped me get something done I needed to do very quickly. You rock!


May 19, 2009 at 9:32 AM // reply »
7,539 Comments

@Curtis,

Glad to help!


Aug 25, 2009 at 8:14 PM // reply »
1 Comments

Thank you for sharing, Ben. Really learned a great deal!


Sep 6, 2009 at 1:04 PM // reply »
7,539 Comments

@Guido,

My pleasure.


Post Comment  |  Ask Ben

Recent Blog Comments
Mar 19, 2010 at 12:50 AM
jQuery Attr() Function Doesn't Work With IMAGE.complete
I just fixed the code. There was a function "watch" inside the function imgLoad. It spammed a lot of errors: Error: missing argument 1 when calling function watch. To fix this: instead of setInt ... read »
Mar 18, 2010 at 10:28 PM
Posting XML SOAP Requests With jQuery
can you please point me to the jquery documentation on the following # // Create our SOAP body content based off of # // the template. # var soapBody = soapTemplate.html().replace( # new RegExp( "\\ ... read »
Mar 18, 2010 at 6:34 PM
Exploring ColdFusion Component Runtime Class Properties And Serialization
@Ben Very useful analyses. Thank you @Elliot Thanks for additional clarification Though, it's quite a shame that getBust() failed...not defined ;) ... read »
Mar 18, 2010 at 5:35 PM
Exploring ColdFusion Component Runtime Class Properties And Serialization
Saving private properties is necessary so that you can "reconstitute" an object on the other side of the wire, or load up a serialized object you saved to disk. If it didn't save the private state o ... read »
Mar 18, 2010 at 4:04 PM
jQuery's Event Triggering, Order Of Default Behavior, And triggerHandler()
Tks! You saved-me today. it can be chained into one statement: $("#x).attr("checked","checked").triggerHandler('click'); ... read »
Mar 18, 2010 at 1:18 PM
Finally Finished Ayn Rand's Atlas Shrugged Audio Book
@joaopft, Not disputing what you say - but... If I understand you correctly, you are saying that Positivism is based on sense experience (what I experience is what is), but Quantum theory states tha ... read »
Mar 18, 2010 at 11:48 AM
Duplicate() Much Faster Than ColdFusion Query-of-Queries
I am working on a massive xml parsing, qofq app to create 2 seperate xml files. I just don't understand the concept/purpose of duplicate function, are you duplicating the data or the row, into a new ... read »
Mar 18, 2010 at 11:22 AM
Exploring ColdFusion Component Runtime Class Properties And Serialization
@Zarko, Ha ha, you know ColdFusion is my first love ;) ... read »