Ask Ben: Formatting A Date Span In ColdFusion

Posted June 25, 2009 at 9:28 AM by Ben Nadel

Tags: ColdFusion, Ask Ben

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.



Reader Comments

Jun 25, 2009 at 10:04 AM // reply »
16 Comments

Pretty nice. Do you get to post some of these your UDFs to cflib.org?


Jun 25, 2009 at 10:24 AM // reply »
11,238 Comments

@William,

I keep meaning to, but am always struggling to find the time.


Jun 25, 2009 at 10:43 AM // reply »
46 Comments

I have been using the DateRangeFormat UDF from 2004:
http://www.cflib.org/udf/DateRangeFormat


Jun 25, 2009 at 10:58 AM // reply »
11,238 Comments

@James,

Looks good. Looks to do just about the same thing with a few more options.


Jun 25, 2009 at 12:11 PM // reply »
40 Comments

or you know, use icu4j's dateIntervalFormat class:

http://icu-project.org/apiref/icu4j/com/ibm/icu/text/DateIntervalFormat.html


Jun 25, 2009 at 12:16 PM // reply »
11,238 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.


Jun 25, 2009 at 12:30 PM // reply »
40 Comments

too many cool java libs to ignore, might as well try to stop a freight train by winking at it.


Jun 25, 2009 at 12:55 PM // reply »
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.


Jun 25, 2009 at 1:40 PM // reply »
40 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.


Jun 25, 2009 at 2:06 PM // reply »
9 Comments

Actually, when I said 'you' I meant it in the plural sense :)

There are times when you cannot beat icu4j - that goes without saying.


Jun 29, 2009 at 12:54 AM // reply »
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.


Jun 29, 2009 at 8:39 AM // reply »
11,238 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.


Jim
Jul 24, 2009 at 9:10 AM // reply »
2 Comments

Nice technique.


Nov 1, 2009 at 11:44 AM // reply »
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


Nov 1, 2009 at 2:47 PM // reply »
11,238 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.



Post A Comment

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.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
May 20, 2013 at 4:38 PM
Using A Dynamic Column Name With ValueList() In ColdFusion
@Dana, Your confusion is well founded, since this is a very confusing features. In fact, it ONLY works if you use array notation. Meaning, that this: arrayToList( query[ "columnName" ] ) ... read »
May 20, 2013 at 4:34 PM
Using A Dynamic Column Name With ValueList() In ColdFusion
I was thinking chicken and the egg, I wouldn't have expected it to work in the valuelist going in I guess. Maybe I just need a beer, long day :) ... read »
May 20, 2013 at 4:29 PM
Using A Dynamic Column Name With ValueList() In ColdFusion
@Dana, That's if you're trying to reference a specific row. In this case, we're trying to reference the entire query column as one cohesive value. So, you are correct that if you wanted to output a ... read »
May 20, 2013 at 4:24 PM
Using A Dynamic Column Name With ValueList() In ColdFusion
I thought when you used array notation to reference queries you always had to have the row or it would throw a similar error as well? ... read »
May 20, 2013 at 11:45 AM
Using jQuery's Animate() Step Callback Function To Create Custom Animations
This is really useful. I found out that you don't actually have to use a dummy css property (surprisingly). To animate a property in a linear-gradient for instance I did this this.css('someLinearGra ... read »
May 20, 2013 at 10:51 AM
Using A Dynamic Column Name With ValueList() In ColdFusion
@Josh, Oh snap! You're totally right! I'm not sure I've ever tried that. I did know that you can call a number of other array-methods on ColdFusion query columns: http://www.bennadel.com/blog/167 ... read »
May 20, 2013 at 10:45 AM
Using A Dynamic Column Name With ValueList() In ColdFusion
@Ben - I believe you can achieve the same functionality with ColdFusion's built in ArrayToList() function. ArrayToList( users[ "id" ] ); ... read »
May 20, 2013 at 10:21 AM
My Experience With AngularJS - The Super-heroic JavaScript MVW Framework
Is there any error logging and handling framework in angularjs, if not then in what way I can do this. ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools