You know those reports that upper management request from time to time? The ones that give you nightmares? Yeah those. Well sometimes they will request quarterly reports so I wanted to share an easy way to define quarterly dates.
So we start with our default variables:
<cfset quarter = 1>
<cfset year = "#year(now())#">
<cfset startmonth = 1>
And here are the functions that create the dates:
<cffunction name="QuarterFirstDate" returnType="date">
<cfargument name="quarternumber" required="yes" type="numeric">
<cfargument name="yr" type="numeric" default="#year(now())#">
<cfargument name="startmonth" type="numeric" default="1">
<cfset firstDate = DateAdd("m",startmonth-1,CreateDate(yr, ((quarternumber-1)*3)+1, "1"))>
<cfreturn firstDate>
</cffunction>
<cffunction name="QuarterLastDate" returnType="date">
<cfargument name="quarternumber" required="yes" type="numeric">
<cfargument name="yr" type="numeric" default="#year(now())#">
<cfargument name="startmonth" type="numeric" default="1">
<cfset lastDate = DateAdd("m",startmonth-1,CreateDate(yr, quarternumber*3, DaysInMonth(CreateDate(yr, quarternumber*3, "1"))))>
<cfreturn lastDate>
</cffunction>
And here is how I define my returned variables to be used in my code:
<cfset startDate = #DateFormat(QuarterFirstDate(quarter, year, startmonth))#>
<cfset endDate = #DateFormat(QuarterLastDate(quarter, year, startmonth))#>
To change the quarters, simply change the default quarter variable define at the top of the page:
<cfset quarter = 1> = 01-Jan-14 - 31-Mar-14
<cfset quarter = 2> = 01-Apr-14 - 30-Jun-14
<cfset quarter = 3> = 01-Jul-14 - 30-Sep-14
<cfset quarter = 4> 01-Oct-14 - 31-Dec-14
nJoy!