An includable select to pick the first day of quarter:

<cfparam name="dtFirst" default="#now()#"><!--- first date in select --->
<cfparam name="selQtrName" default="selQtr"><!--- name attr of select --->
<cfparam name="selQtrCss" default=""><!--- special style css --->
<cfparam name="howManyOpts" default="10"><!--- how many quarters to go back --->
<cfparam name="selQtrDefault" default=""><!--- default selected date  current qtr: #right("0"&(1+(quarter(now())-1)*3), 2)#/01/#year(now())# --->
<cfset raQtoDt=["01/01/","04/01/","07/01/","10/01/"]>
<cfset qtrZWX=quarter(dtFirst)>
<cfset yrZWX=year(dtFirst)>
<select name="#selQtrName#" id="#selQtrName#" style="#selQtrCss#">
<cfloop from="1" to="#howManyOpts#" index="idxZWX">
    <option value="#raQtoDt[qtrZWX]##yrZWX#"
    <cfif len(selQtrDefault) and selQtrDefault eq "#raQtoDt[qtrZWX]##yrZWX#">selected</cfif>>#raQtoDt[qtrZWX]##yrZWX#</option>
    <cfset qtrZWX=qtrZWX-1>
    <cfif qtrZWX eq "0">
        <cfset qtrZWX=4>
        <cfset yrZWX=yrZWX-1>

1 Answer 1


My primary suggestion would be to try and make the code more intuitive.

  1. While the current variable names are syntactically valid, they are a bit cryptic IMO. Consider using slightly longer and more descriptive names throughout the code. That will make it easier to understand when you come back to it a few months down the road.

  2. Use date objects (not strings), when generating the quarter dates. Start by creating the initial quarter date with the CreateDate() function. (Use this formula to calculate the first quarter of the month). Then use DateAdd() to derive the remaining quarters. For comparisons, use DateCompare().

  3. Consider using a non-ambiguous format like yyyy-mm-dd for the list value. You can still display a more user friendly format for the list text.

  4. Be sure to explicitly scope all variables to avoid accidental scope conflicts. For example, if the variables are submitted via URL, use url.howManyOpts instead of just howManyOpts

<cfparam name="url.startDate" default="#now()#">
<cfparam name="url.howManyOpts" default="10">
<cfparam name="url.selQuarter" default="">

<!--- create starting quarter date --->
<cfset quarterMonth = Int((month(url.startDate) - 1) / 3) * 3 + 1>
<cfset currQuarter = createDate(year(url.startDate), quarterMonth, 1)>

<!--- if selected quarter was not defined, set a default --->
<cfif !len(trim(url.selQuarter))>
    <cfset url.selQuarter = currQuarter>

<select name="selQuarter">
   <cfloop from="1" to="#url.howManyOpts#" index="x">
       <!--- separated for clarity --->
       <cfset optValue   = dateFormat(currQuarter, 'yyyy-mm-dd')>
       <cfset optText    = dateFormat(currQuarter, 'mm-dd-yyyy')>
       <cfset isSelected = dateCompare(currQuarter, url.selQuarter) eq 0>

       <option value="#optValue#" <cfif isSelQuarter>selected</cfif>>#optText#</option>

       <!--- move to next quarter --->      
       <cfset currQuarter = dateAdd("m", 3, currQuarter)>
  • 1
    \$\begingroup\$ nice updates - more readable AND robust \$\endgroup\$
    – gordon
    Commented Apr 19, 2017 at 22:28

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.