UDF Series: LastDayOfMonth()

UDF Series: LastDayOfMonth()

Starting today and continuing sporadically, I will release some of my UDF library to the public. None of it is ultimately earth-shattering nor are all of them unique (CFLib.org is a great collection), but I realized that I do have some decent functions or variations of existing functions that could be helpful to other developers.

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?


About the Author

Rob has been in web development for over 10 years, 9 of which have been focused on being a ColdFusion Application Developer. Project Management, eCommerce Consulting, and Marketing Consulting are also in the quiver. If you like what I have to say, consider following me on Twitter or reading more about me here: About Rob O'Brien