Skip to main content
Ben Nadel at BFusion / BFLEX 2009 (Bloomington, Indiana) with: Zach Stepek
Ben Nadel at BFusion / BFLEX 2009 (Bloomington, Indiana) with: Zach Stepek

Ask Ben: Formatting A Date Span In ColdFusion

By
Published in , Comments (15)

I'm new to CF. I have values for startDate and endDate. Let's say my startDate is June 12, 2009 and my endDate is June 30, 2009. How do I display it to look like 'June 12-30, 2009'. And if the months are different for startDate and endDate, how do I display 'June 12 - December 12, 2009'. Thank you for your help.

In ColdFusion, there's no inherent way to format a date span. However, using ColdFusion's DateFormat() method, we can easily come up with some logic to format the span between two dates. Looking at your question, I see three different date span formatting scenarios:

  1. Two dates in different years.

  2. Two dates in the same year, but different months.

  3. Two dates in the same year and the same month.

Each of these three scenarios will require a different format. If we start to think in terms of date masks (which are used by ColdFusion's DateFormat() method), we can see the above three scenarios as using these three date masks:

  1. mmmm d, yyyy - mmmm d, yyyy

  2. mmmm d - mmmm d, yyyy

  3. mmmm d-d, yyyy

Now, of course we can't use DateFormat() to format two dates at the same time - that's where our logic comes into play. And, since this is something that might be called multiple times (at least three to test with), I am going to wrap the logic up into a ColdFusion user defined function (UDF) such that we don't have to repeat our logic every time we want to perform this action:

<cffunction
	name="DateSpanFormat"
	access="public"
	returntype="string"
	output="false"
	hint="I take two dates and format their time span.">

	<!--- Define arguments. --->
	<cfargument
		name="From"
		type="any"
		required="true"
		hint="I am the from date/time."
		/>

	<cfargument
		name="To"
		type="any"
		required="true"
		hint="I am the to date/time."
		/>

	<!--- Define the local scope. --->
	<cfset var LOCAL = {} />

	<!---
		Check to see if the dates are the same year. If they are,
		then we can format a compact time span. If they are not
		the same year, then we have not special format.
	--->
	<cfif (Year( ARGUMENTS.From ) EQ Year( ARGUMENTS.To ))>

		<!---
			Now that we know the dates are the same year, let's
			see how compact we can format the span. If they are
			the same month, we can go even smaller.
		--->
		<cfif (Month( ARGUMENTS.From ) EQ Month( ARGUMENTS.To ))>

			<!---
				Return same-month span.
				Example: June 12-30, 2009
			--->
			<cfreturn (
				DateFormat( ARGUMENTS.From, "mmmm d" ) &
				"-" &
				DateFormat( ARGUMENTS.To, "d, yyyy" )
				) />

		<cfelse>

			<!---
				Return same-year span.
				Example: June 12 - December 12, 2009
			--->
			<cfreturn (
				DateFormat( ARGUMENTS.From, "mmmm d" ) &
				"-" &
				DateFormat( ARGUMENTS.To, "mmmm d, yyyy" )
				) />

		</cfif>

	<cfelse>

		<!--- Just return a two-part date span format. --->
		<cfreturn (
			DateFormat( ARGUMENTS.From, "mmmm d, yyyy" ) &
			" - " &
			DateFormat( ARGUMENTS.To, "mmmm d, yyyy" )
			) />

	</cfif>
</cffunction>

As you can see, the UDF takes the two dates for which we want to format the time span. It then checks to see which of the above three date span scenarios the two dates fall into and returns the appropriate date span string. While ColdFusion's DateFormat() method cannot format two dates at the same time, you can see in the above UDF that we are using DateFormat() to return partial date formats which we then concatenate to form a full date span value.

To test this, I tried inputting date spans for each of the three scenarios above:

<!--- Output some testing. --->
<cfoutput>

	<!--- Different years. --->
	#DateSpanFormat( "12/10/2008", "2/15/2009" )#<br />
	<br />

	<!--- Same year, different months. --->
	#DateSpanFormat( "1/10/2009", "2/15/2009" )#<br />
	<br />

	<!--- Same year, same month. --->
	#DateSpanFormat( "2/10/2009", "2/15/2009" )#<br />

</cfoutput>

When I run the above code, I get the following output:

December 10, 2008 - February 15, 2009

January 10-February 15, 2009

February 10-15, 2009

ColdFusion makes working with dates and times extremely easy, but sometimes we have to build a little logic around what it gives us. I hope that helps.

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

Reader Comments

15,841 Comments

@PaulH,

Ha ha :) I like it - but in the interest of the new programmer who asked, I feel like we should try to avoid Java integration.

9 Comments

Very nice. We use ICU4J but if you look at the implementation examples, it's much more complex.
So, if you know you won't need the i18n aspect then this is a neat solution.

41 Comments

@ciaran that's pretty funny, "if" i looked at the implementation examples.

actually it's no more than what ben's done with his function but w/out all the brain sweat (it's really only 3-4 lines of code). while ben's only written a few lines of code, i imagine quite a bit of thought went into this. and of course, there's a richness in icu4j that's hard to match. but you already know that.

1 Comments

Hey Ben,

Thanks for answering my Date Formatting question. As a CF beginner, I was afraid you were going to give me a complicated answer that I won't be able to follow (no java), but I actually understood it. It was logical and simple.

15,841 Comments

@Helena,

Glad that I could help out! I try to explain things as thoroughly as possible. If you get stumped again, drop me a line.

33 Comments

Hi Ben,

I have a date_entry column in my database...
I need to enetr the current date in dddd/yy/mm
format.

I have used the dateformat while inserting into the database ,requirement is such a way like i am getting the date value from another query and inserting the same to database in my query...But the problem is I am unable to remove the 'ts'..I mean even 'ts' is also getting inserted into the table along withe date as shown here...{'ts' 20009/10/09} ...I just need to insert 20009/10/09...

Please advise Ben...

Thanks

15,841 Comments

@Abhijit,

I am a bit confused by your notation. Why do you need four days (dddd)? And, in the database what does "20009" stand for in terms of days?

As far as the "ts" is concerned, if you really want to remove that, you might have to change the data type of the column to be a varchar value. I am not exactly sure why you would want to remove the TS - you can simply format the date when you pull it out of the database.

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