Not yet rated

Problem

I want to pass in a date and and have the date which represents the last day of that month returned to me.

Solution

Using the coldfusion daysinmonth() function I can get the last day of the month. I can use the year() and month() functions to get the other parts of the date and then use createdate to put it all back together.

Detailed explanation

Here is the solution using coldfusion tags:

<CFSET myDate = now() />
<CFSET yr = year(myDate) />
<CFSET m = month(myDate) />
<CFSET d = daysinmonth(myDate) />
<CFSET lastDayofMonth = createdate(yr,m,d) />

To get the last day of last month, simply replace the first line above with the following:

<CFSET myDate = dateadd("m",-1,now()) />

You can choose any date for the myDate variable and the lastdayofmonth variable will return the date for the last day of the month from the myDate value.


+
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.

Report abuse

Related recipes