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 »
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 »
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 »
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
Comments (5) | Post Comment | Ask Ben | Permalink | Other Searches | Print Page
Very Strange Spam Site / Link Farm?
ColdFusion Calendar Event Scripting - The Theory Of Handling Event Updates And Exceptions
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?
Posted by duncan on Jul 31, 2007 at 10:26 AM
@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.
Posted by Ben Nadel on Jul 31, 2007 at 11:29 AM
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.
Posted by sherrardb on Aug 2, 2007 at 1:36 PM
@Sherrardb,
Yes, that would accomplish the same thing, so if you find that method more intuitive, by all means go for it.
Posted by Ben Nadel on Aug 5, 2007 at 5:16 PM
Thanks Ben, Great solution. You saved me from having to think too hard ;)
Posted by Chris W Nickel-Felton on Aug 7, 2007 at 12:39 AM