Skip to main content
Ben Nadel at cf.Objective() 2011 (Minneapolis, MN) with: Steve Withington
Ben Nadel at cf.Objective() 2011 (Minneapolis, MN) with: Steve Withington

ColdFusion UDF For Retrieving Surrounding Dates

By
Published in Comments (2)

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.

Want to use code from this post? Check out the license.

Reader Comments

15 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

I believe in love. I believe in compassion. I believe in human rights. I believe that we can afford to give more of these gifts to the world around us because it costs us nothing to be decent and kind and understanding. And, I want you to know that when you land on this site, you are accepted for who you are, no matter how you identify, what truths you live, or whatever kind of goofy shit makes you feel alive! Rock on with your bad self!
Ben Nadel