What's the most efficient way to calculate the last day of the CURRENT quarter?
Example: given the date 3/5/09, I want to return 3/31/09.
Platform is ColdFusion and SQL Server
What's the most efficient way to calculate the last day of the CURRENT quarter?
Example: given the date 3/5/09, I want to return 3/31/09.
Platform is ColdFusion and SQL Server
This could use some refactoring, but should get the basic idea across.
<cffunction name="lastDayOfQuarter">
<cfargument name="d" default="#now()#">
<cfif month(d) lte 3>
<cfreturn createDate(year(d),03,31)>
</cfif>
<cfif month(d) lte 6>
<cfreturn createDate(year(d),06,30)>
</cfif>
<cfif month(d) lte 9>
<cfreturn createDate(year(d),9,30)>
</cfif>
<cfreturn createDate(year(d),12,31)>
</cffunction>
select dateadd(dd,-1,dateadd(qq,1,DATEADD(qq, DATEDIFF(qq,0,), 0)))
actually if you mean current quarter: select dateadd(dd,-1,dateadd(qq,1,DATEADD(qq, DATEDIFF(qq,0,getdate()), 0)))
This answer uses the built in Quarter and DaysInMonth functions:
#createDate(year(now()), (quarter(now())*3), daysInMonth(createDate(year(now()), quarter(now())*3,1)) )#
It might be easier to read if its broken out a bit.
EDIT (@Sam Farmer: I took the liberty to transform your suggestion into a CF function)
<cffunction name="LastOfQuarter" returntype="date" output="no" access="public">
<cfargument name="d" type="date" required="no" default="#Now()#">
<cfset d = CreateDate(Year(d), Quarter(d) * 3, 1)>
<cfreturn DateAdd("d", d, DaysInMonth(d) - 1)>
</cffunction>
Could you clarify if you want it to be the last business day or the last actual day of the quarter.
Ran into that once. It might be an additional step you need to calculate for.