ColdFusion DateTimeFormat() Utility Function

Posted May 22, 2007 at 7:31 AM

Tags: ColdFusion

I love ColdFusion's DateFormat() and TimeFormat() functions; they are hugely useful. And, most of the time, I use them independently of each other. But, often enough, I use DateFormat() followed by TimeFormat(). Wouldn't it be cool if ColdFusion had a DateTimeFormat() function that would accept both a date and a time mask? I think it would be, and here's what it might look like:

 Launch code in new window » Download code as text file »

  • <cffunction
  • name="DateTimeFormat"
  • access="public"
  • returntype="string"
  • output="false"
  • hint="Formats the given date with both a date and time format mask.">
  •  
  • <!--- Define arguments. --->
  • <cfargument
  • name="Date"
  • type="date"
  • required="true"
  • hint="The date/time stamp that we are formatting."
  • />
  •  
  • <cfargument
  • name="DateMask"
  • type="string"
  • required="false"
  • default="dd-mmm-yyyy"
  • hint="The mask used for the DateFormat() method call."
  • />
  •  
  • <cfargument
  • name="TimeMask"
  • type="string"
  • required="false"
  • default="h:mm TT"
  • hint="The mask used for the TimeFormat() method call."
  • />
  •  
  • <cfargument
  • name="Delimiter"
  • type="string"
  • required="false"
  • default=" at "
  • hint="This is the string that goes between the two formatted parts (date and time)."
  • />
  •  
  •  
  • <!---
  • Return the date/time format by concatenating the date
  • and time formatting separated by the given delimiter.
  • --->
  • <cfreturn (
  • DateFormat(
  • ARGUMENTS.Date,
  • ARGUMENTS.DateMask
  • ) &
  •  
  • ARGUMENTS.Delimiter &
  •  
  • TimeFormat(
  • ARGUMENTS.Date,
  • ARGUMENTS.TimeMask
  • )
  • ) />
  • </cffunction>

Using the above ColdFusion user defined function, you could easily format date/time values:

 Launch code in new window » Download code as text file »

  • <!---
  • DateTimeFormat called with all possible
  • defaulted arguments.
  • --->
  • #DateTimeFormat(
  • Now()
  • )#
  •  
  •  
  • <!---
  • DateTimeFormat called with explicit date
  • and time masks and default delimiter.
  • --->
  • #DateTimeFormat(
  • Now(),
  • "mmm d, yyyy",
  • "h:mm TT"
  • )#
  •  
  •  
  • <!---
  • DateTimeFormat called with explicit date and
  • time masks as well as an explicit delimiter.
  • --->
  • #DateTimeFormat(
  • Now(),
  • "mmm d, yyyy",
  • "h:mm TT",
  • " at the time of "
  • )#

The above code would give us the following output:

22-May-2007 at 7:24 AM
May 22, 2007 at 7:24 AM
May 22, 2007 at the time of 7:24 AM

I'm sure this has been done before, but I just really hope that one day Adobe adds it to the ColdFusion built-in function list.

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Permalink  |  Other Searches  |  Print Page



Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

Reader Comments

May 22, 2007 at 7:47 AM // reply »
11 Comments

Yup, Ray did it back in 2001 and has it on his cflib.org site http://www.cflib.org/udf.cfm?ID=134

cflib.org hasn't got much press lately and it's easy to forget the wealth of udf's on the site.


May 22, 2007 at 8:01 AM // reply »
6,516 Comments

@Chris,

It's funny, right after I posted this, I did a quick Google search to see if this was out there already (as I assumed it was) and there it was, CFLib as the second search result.

2001... I guess that means I'm only 6 years behind the Jedi :) The force is weak with me.


May 22, 2007 at 9:34 AM // reply »
207 Comments

Heh, people _do_ forget about CFLib, so it is no big deal. ;) I need to get off my ass and release that new version that I've been talking about since 2004. ;)


May 22, 2007 at 9:45 AM // reply »
95 Comments

Ben, good job anyway. If nothing else you made me aware of the existing function at cflib. Thanks!


May 22, 2007 at 9:47 AM // reply »
6,516 Comments

@Ray,

Some of the best meals I have ever eaten were cooking over many many hours... that doesn't exactly translate to web sites, other than to say, good things are worth the wait.


May 22, 2007 at 11:58 AM // reply »
56 Comments

@Ray,

Are you ever going to just integrate cflib into riaforge?


May 22, 2007 at 12:11 PM // reply »
207 Comments

No plans. I really look at RIAForge as more for 'projects', not single CFCs/UDFs/tags. Now that is NOT official RIAForge policy. Shoot, even I have a single CFC up there I believe. But in GENERAL those are my thoughts. (Have to treat carefully here. I admin both sites, but Adobe is the official owner of RIAForge. Etc etc etc.)


Del
Jul 19, 2009 at 5:39 PM // reply »
1 Comments

Thanks a lot Ben this was really helpful.

Changed the Defaults to mm/dd/yyyy HH:mm:ss

It worked like a charm.


Jul 19, 2009 at 5:40 PM // reply »
6,516 Comments

@Del,

Glad you are finding this useful.


Post Comment  |  Ask Ben

Recent Blog Comments
Nov 22, 2009 at 1:45 AM
Streaming Text Using ColdFusion's CFContent Tag And The Variable Attribute
The reason you would want to do this is to stream. Ack json/xml files to ria clients I used thus technique before because putting json in response stream causes debugging info to come thru As well a ... read »
Nov 21, 2009 at 6:47 PM
Hal Helms - Real World Object Oriented Development, Sarasota - Day Five
@charlie griefer, Thank you.. ... read »
Nov 21, 2009 at 5:15 PM
Using ColdFusion Structures To Remove Duplicate List Values
@Jose Galdamez, Oh heh yeah I didn't paste the whole code. I should have defined the vars -- my bad. It's fixed thou. Thanks. ... read »
Nov 21, 2009 at 4:49 PM
Styling The ColdFusion 8 WriteToBrowser CFImage Output
Great work yet again Ben! Whilst I didn't use this whole code, I copied some of your regex code for a similar problem with the lack of an alt attribute and unescaped ampersands in CFIMAGE for Railo 3 ... read »
Nov 21, 2009 at 1:13 PM
My First ColdFusion Builder Extension - Encrypting And Decrypting CFM / CFC Files
@Ben, Because I am pedantic, I just want to make sure that everyone knows there is absolutely no encryption going on. There is only encoding and obfuscation. The cfencode tool only obfuscates your C ... read »
Nov 21, 2009 at 12:28 PM
Using ColdFusion Structures To Remove Duplicate List Values
@Jody I can't seem to get your code sample to work. If you are still having problems, try this code out and see if it gets you what you wanted. <!--- Comma delimited list with various duplicates ... read »
Nov 21, 2009 at 11:03 AM
Groovy Operator Overloading Does Not Work In The ColdFusion Context
Hi Ben, Thanks for this informative post. Now I am reading ur old posts too ... read »
Nov 21, 2009 at 10:56 AM
HostMySite.com Has The Best ColdFusion Hosting
@Mehul, Yes very nice people, however several downtimes per day which was not acceptable. Hence we had to move out. I am glad you are having good luck with them so far. ... read »