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  |  Other Searches  |  Print Page





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
Mar 18, 2010 at 10:28 PM
Posting XML SOAP Requests With jQuery
can you please point me to the jquery documentation on the following # // Create our SOAP body content based off of # // the template. # var soapBody = soapTemplate.html().replace( # new RegExp( "\\ ... read »
Mar 18, 2010 at 6:34 PM
Exploring ColdFusion Component Runtime Class Properties And Serialization
@Ben Very useful analyses. Thank you @Elliot Thanks for additional clarification Though, it's quite a shame that getBust() failed...not defined ;) ... read »
Mar 18, 2010 at 5:35 PM
Exploring ColdFusion Component Runtime Class Properties And Serialization
Saving private properties is necessary so that you can "reconstitute" an object on the other side of the wire, or load up a serialized object you saved to disk. If it didn't save the private state o ... read »
Mar 18, 2010 at 4:04 PM
jQuery's Event Triggering, Order Of Default Behavior, And triggerHandler()
Tks! You saved-me today. it can be chained into one statement: $("#x).attr("checked","checked").triggerHandler('click'); ... read »
Mar 18, 2010 at 1:18 PM
Finally Finished Ayn Rand's Atlas Shrugged Audio Book
@joaopft, Not disputing what you say - but... If I understand you correctly, you are saying that Positivism is based on sense experience (what I experience is what is), but Quantum theory states tha ... read »
Mar 18, 2010 at 11:48 AM
Duplicate() Much Faster Than ColdFusion Query-of-Queries
I am working on a massive xml parsing, qofq app to create 2 seperate xml files. I just don't understand the concept/purpose of duplicate function, are you duplicating the data or the row, into a new ... read »
Mar 18, 2010 at 11:22 AM
Exploring ColdFusion Component Runtime Class Properties And Serialization
@Zarko, Ha ha, you know ColdFusion is my first love ;) ... read »
Mar 18, 2010 at 11:15 AM
Exploring ColdFusion Component Runtime Class Properties And Serialization
Hi Ben, nice to have you back! I already gave up on you, thinking you'll write about jQuery and iPhone for the rest our our lives! :) ... read »