ColdFusion UDF For Retrieving Surrounding Dates
Posted May 7, 2007 at 11:22 PM
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:
| | | | ||
| | ![]() | | ||
| | | |
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
Newer Post
Using A Name Suffix In ColdFusion's CFMail Tag
Older Post
Ask Ben: Truncating The Event Titles In A Calendar Display
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
Thank you who writes with that this code! Very helped!





