Ben Nadel
On User Experience (UX) Design, JavaScript, ColdFusion, Node.js, Life, and Love.
I am the chief technical officer at InVision App, Inc - a prototyping and collaboration platform for designers, built by designers. I also rock out in JavaScript and ColdFusion 24x7.
Meanwhile on Twitter
Loading latest tweet...
Ben Nadel at RIA Unleashed (Nov. 2009) with: Chris Lee

ColdFusion UDF For Retrieving Surrounding Dates

By Ben Nadel on
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:

  • <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:

  • <!---
  • 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.




Reader 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

Reply to this Comment

Post A Comment

You — Get Out Of My Dreams, Get Into My Comments
Live in the Now
Oops!
Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.