What's the fastest way in ColdFusion to get the first and last day of the quarter?

What's the fastest way in ColdFusion to get the first and last day of the quarter?

There is no built-in function for this.

+2


source to share


5 answers


First day of the quarter:

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

      



Last day of the quarter:

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

      

+4


source


My feeling is that your question may be more complex than it sounds ... for most purposes these values ​​are a known set - no need to compute:

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 is no real need for a function to do this - determining what date a quarter is falling is a simple series of if statements (psuedocode):



if date> October 1, then Q4 else if date> July 1, then Q3 else if date> April 1, then Q2 else if date> January 1, then Q1

(In this case, you check back to check the most restrictive match first. Although, as Kimway points out, there is a function that runs in CFML.)

This should be essentially the same for other "quarterly" systems if these dates are not calculated in some way.

If I missed a mark, feel free to add comments to clarify.

+5


source


The problem is that β€œquarter” is a relative term, while many organizations follow the default quarterly breakdown throughout the year from January 1st to December 31st, many other organizations follow other quarters.

For example, most retail organizations. In particular, those who depend on Christmas don't want to waste time on financial results for the quarter / year in December. They also want the entire holiday season (including 2 weeks after that) on the same books as the rest of the season. Therefore, for them the "year" begins on February 1st.

The US federal government and most states begin their fiscal year on October 1 due to the way legislatures, elections, and budgets work.

So the only function that always worked for just one quarter of the layout would never fire. Any of the functions listed in the other answers are ok, I'm sure as long as your program only works with one set. But if your coding is used in a general application, you can customize it.

+3


source


It looks like there is a function to define the Quarter, based on that can you hardcode them?

+2


source


I don't think there are any built-in functions for this - it is not clear if you are looking for a numeric day of the month or a string day of the month (like Monday). Anyway, it might be a little over-the-top - two functions that require a quarter integer value and return the first and last quarter dates 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: update to indicate start month of month

+2


source







All Articles