ColdFusion UDF For Retrieving Surrounding Dates

Posted May 7, 2007 at 11:22 PM

Tags: ColdFusion

Earlier today, Sam suggested that I wrap at least some of my surrounding date algorithms in a ColdFusion user defined function. Here is my UDF, GetSurroundingDates(), that wraps all those dates up in a struct and returns it:

 Launch code in new window » Download code as text file »

  • <cffunction
  • name="GetSurroundingDates"
  • access="public"
  • returntype="struct"
  • output="false"
  • hint="Returns the start and end dates of all same-year dates relavent to the given date.">
  •  
  • <!--- Define arguments. --->
  • <cfargument
  • name="Date"
  • type="date"
  • required="true"
  • />
  •  
  •  
  • <!--- Set up local scope. --->
  • <cfset var LOCAL = StructNew() />
  •  
  • <!---
  • Start out fixing the passed in date. Fixing it will
  • remove the time and result in a date-only values. This
  • value will be echoed back in the resultant struct.
  • --->
  • <cfset LOCAL.Date = DateFormat( ARGUMENTS.Date ) />
  •  
  • <!--- Get the start and end date of the week. --->
  • <cfset LOCAL.WeekStart = DateFormat( LOCAL.Date - DayOfWeek( ARGUMENTS.Date ) + 1 ) />
  • <cfset LOCAL.WeekEnd = DateFormat( LOCAL.WeekStart + 6 ) />
  •  
  • <!--- Get the start and end of the month. --->
  • <cfset LOCAL.MonthStart = DateFormat( LOCAL.Date - Day( LOCAL.Date ) + 1 ) />
  • <cfset LOCAL.MonthEnd = DateFormat( LOCAL.Date + (DaysInMonth( LOCAL.Date ) - Day( LOCAL.Date )) ) />
  •  
  • <!--- Get the start and end of the calendar month. --->
  • <cfset LOCAL.CalendarMonthStart = DateFormat( LOCAL.MonthStart - DayOfWeek( LOCAL.MonthStart ) + 1 ) />
  • <cfset LOCAL.CalendarMonthEnd = DateFormat( LOCAL.MonthEnd + (7 - DayOfWeek( LOCAL.MonthEnd )) ) />
  •  
  • <!--- Get the start and end of the year. --->
  • <cfset LOCAL.YearStart = DateFormat( CreateDate( Year( LOCAL.Date ), 1, 1 ) ) />
  • <cfset LOCAL.YearEnd = DateFormat( CreateDate( Year( LOCAL.Date ), 12, 31 ) ) />
  •  
  •  
  • <!--- Return the surrounding dates. --->
  • <cfreturn LOCAL />
  • </cffunction>

Calling it:

 Launch code in new window » Download code as text file »

  • <!---
  • Get today as the given date. We don't care about
  • fixing the date as the ColdFusion UDF will take
  • care of that for us.
  • --->
  • <cfset dtToday = Now() />
  •  
  • <!--- Dump out resultant struct. --->
  • <cfdump
  • var="#GetSurroundingDates( dtToday )#"
  • label="Surronding Dates - #DateFormat( dtToday )#"
  • />

... we get the following output:


 
 
 

 
Get Surrounding Dates In ColdFusion  
 
 
 

I put the DateFormat() calls inside of the GetSurroundingDates() ColdFusion UDF for ease of use, but honestly, these are just overkill to me. I enjoy working with numeric dates so I don't care that dates coming out of it would be numeric (would fail IsDate() method calls). Once you get comfortable with numeric dates, I would recommend stripping out those DateFormat() calls as they do nothing significant but add to processing overhead.

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Permalink  |  Other Searches  |  Print Page




Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

Reader Comments

May 8, 2007 at 7:01 AM // reply »
13 Comments

good on yer, Ben,

the next step is submitting the UDF to cflib.org so it'll spread around the world for evermore (well, that's perhaps going a bit far but you get the idea...)

just a thought


May 8, 2007 at 7:32 AM // reply »
9 Comments

Thank you who writes with that this code! Very helped!


Post Comment  |  Ask Ben

Recent Blog Comments
Nov 20, 2009 at 11:32 PM
Five Months Without Hungarian Notation And I'm Loving It
I've used headless camel case for years for not only ColdFusion variables, but also SQL tables and fields... pretty much everything involving code. I also subscribe to the "don't abbreviate and clea ... read »
Nov 20, 2009 at 11:00 PM
Five Months Without Hungarian Notation And I'm Loving It
@Marcel, Yeah, I always err on the side of longer but more readable variable names. As for the camel casing of CF methods and the headless camel casing of custom items, I get around this by always ... read »
Nov 20, 2009 at 10:56 PM
Five Months Without Hungarian Notation And I'm Loving It
I use the following and love it: my.namespace.MyComponents.functionMethodsOrUDF() CONSTANT_VALUES_OR_PROPERTIES One thing I always try is to CamelCaseBuiltInColdFusionFunctions() so others can tell ... read »
Nov 20, 2009 at 5:38 PM
Learning ColdFusion 8: CFImage Part I - Reading And Writing Images
Hi Ben, Great article. I've been looking around to see if ColdFusion image engine can programatically create the following "wrap around" effect: http://www.creativepro.com/article/photoshop-s-she ... read »
Nov 20, 2009 at 5:35 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
@Dave: I talked to Gert he suggested: <cfhttp method="get" url="http://{some cf website}" result="stuff" addtoken="yes" /> Note the addition of cfhttp attribute addtoken. That should persist y ... read »
Nov 20, 2009 at 5:23 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
@Todd, Ahh, gotcha, yeah that makes sense. ... read »
Nov 20, 2009 at 5:17 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
Ben, sorry if I didn't make this clear. You can make it work like that if you want, just put <cfset session.foo = 1> (and <cfset application.foo = 1>) in your OnRequestStart() and it reve ... read »