To start things off, here's a very simple function that will return the last day of any given month. It's easy, straightforward and very useful. I'm surprised that it's not directly included in CF.
<cffunction name="LastDayOfMonth" access="public" returntype="date"> <cfargument name="Date" type="date" required="yes"> <cfreturn CreateDate(year(Arguments.Date),month(Arguments.Date),DaysInMonth(Arguments.Date))> </cffunction>
In order to use it, just pass in any valid date value:
<cfset LastDay = LastDayOfMonth(now())> <cfset LastDay = LastDayOfMonth("3/17/2009")> <cfset LastDay = LastDayOfMonth("March 17, 2009")>
And you'll get a nice and tidy date value back:
{ts '2009-03-31 00:00:00'}
That's all there is to it. Like I said, nothing earth-shattering. Hopefully just helpful knowledge.
Has this solution saved you time?
I'm a firm believer in sharing knowledge for the sake of helping someone else. No strings attached. However, we all have bills to pay and hosting to maintain. If you feel like this post has saved you even an hour of development time, would you consider donating that hour to me?