3

What is the quickest way in ColdFusion to get first and last day of quarter?

There doesn't seem to be a built in function for this.

6 Answers 6

5

First day of quarter:

FirstDayOfQuarter = CreateDate(year, (quarter-1)*3 + 1, 1)

Last day of quarter:

LastDayOfQuarter = DateAdd("d", -1, DateAdd("m", 3, FirstDayOfQuarter))
5

I have a feeling that your question might be more complex that it appears... for most purposes these values are a known set - no need to calculate:

Quarters:

January 1 - March 31 April 1 - June 30 July 1 - September 30 October 1 - December 31

Since the set is known in advance there's no real need for a function for this - determine in which date a quarter falls is a simple series of "if" statements (psuedocode):

if date > Oct 1 then Q4 else if date > Jul 1 then Q3 else if date > Apr 1 then Q2 else if date > Jan 1 then Q1

(You do the check backwards in this case to check for the most restrictive match first. Although, as Kimvais points out, there is a function to do exactly that already in CFML.)

It should be essentilly the same for other "quarter" systems unless those dates are calculated in some way.

If I've missed the mark feel free to add comment to clarify.

2
  • 1
    Therefore, given QuarterMonthFirst as array of values [1,4,7,10] and QuarterMonthLast as array [1,1,1,1] then the calculation to get first day of the quarter is: CreateDate(DatePart("yyyy",targetDate),QuarterMonthFirst[Quarter(targetDate)],QuarterDayFirst[Quarter(targetDate)]) Given similar array defintions QuarterMonthLast and QuarterDayLast, the last day of the quarter is: CreateDate(DatePart("yyyy",targetDate),QuarterMonthLast[Quarter(targetDate)],QuarterDayLast[Quarter(targetDate)])
    – Mead
    Oct 5, 2009 at 12:25
  • I may be mistaken, but I think you have this backwards. I'm not looking for the quarter that a given date falls in, I'm looking for the start and end dates for a given quarter. Oct 13, 2009 at 10:03
3

The problem is that "quarter" is a relative term, while many organizations follow the default quarter breakdown of a year starting Jan 1 through Dec 31, many other organizations follow other quarters.

For example most retail organizations. Particularly those that depend on Christmas, don't want to be spending time doing end of quarter/year financials in december. They also want the entire holiday season (including the 2 weeks afterwards) on the same books as the rest of the season. So for them the "year" begins Feb 1.

The U.S. federal government and most of the states begin their financial year Oct 1 because of the way the legislature, elections and budgets work.

So a single function that always worked off of just one quarter layout would never work. Any of the functions listed in the other answers are fine I'm sure as long as your program is only dealing with one set. But if your coding a general use application then you may want to make it configurable.

2

Looks like there is a function for finding out the Quarter, based on this you could hardcode these?

1
  • That's pretty much what we've always done. I was just looking for something simple and built in enough as to not require an extra function definition. Oct 5, 2009 at 12:20
2

I don't think there are built in functions for this - it's not clear whether you're looking for the numeric day of month or the string day of month (e.g. Monday). Anyway, this may be a little over-the-top - two functions which require an integer quarter value, and return the first and last dates of the quarter for further manipulation:

<cffunction name="QuarterFirstDate" returnType="date">
    <cfargument name="quarternumber" required="yes" type="numeric">
    <cfargument name="yr" type="numeric" default="2009">
    <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="2009">
    <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>

<cfset year = "2009">
<cfset startmonth = "1">

<cfloop index="quarter" from="1" to="4">
    <cfoutput>
        <h2>Quarter #quarter#</h2>
        #DateFormat(QuarterFirstDate(quarter, year, startmonth))#, day #DayOfYear(QuarterFirstDate(quarter, year, startmonth))#, #DayOfWeekAsString(DayOfWeek(QuarterFirstDate(quarter, year, startmonth)))#<br />
        #DateFormat(QuarterLastDate(quarter, year, startmonth))#, day #DayOfYear(QuarterLastDate(quarter, year, startmonth))#, #DayOfWeekAsString(DayOfWeek(QuarterLastDate(quarter, year, startmonth)))#<br />
    </cfoutput>
</cfloop>

edit: updated to allow a quarter start month to be specified

0

How about?

<cfset dt = now()>

<cfset DateLastQuarter = DateAdd("m",-3,dt)> <!--- Any date three months ago falls in previous quarter --->
<cfset quarterNumber = (month(DateLastQuarter)-1)\3+1> <!--- range from quarter 1-4 --->
<cfset StartQuarterMonth = (quarterNumber-1)*3+1> <!--- start of quarter month-number where Jan=1 (Mathematical magic) --->

<cfset LastQuarterFrom = CreateDate(year(DateLastQuarter),StartQuarterMonth,1)>
<cfset LastQuarterTo = DateAdd("d",-1,DateAdd("m",3,LastQuarterFrom))> <!--- the day before three months later --->

this above returns the first date in the previous quarter in LastQuarterFrom and the last date in the previous quarter in LastQuarterTo

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.