ColdFusion DateTimeFormat() Utility Function

Posted May 22, 2007 at 7:31 AM by Ben Nadel

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:

  • <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:

  • <!---
  • 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.



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 »
11,314 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 »
321 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 »
11,314 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 »
321 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 »
11,314 Comments

@Del,

Glad you are finding this useful.


Jul 12, 2010 at 7:53 PM // reply »
1 Comments

Thanks for the function to format date and time together. However I found a similar method if you just want to display the date and time.
By doing this:
#dateformat(created_date, "dd-MMM-yyyy")# #timeformat(created_date, "h:mm:ss tt")#

Hope this helps out.


Jul 18, 2010 at 12:18 PM // reply »
11,314 Comments

@Deepesh,

This function was actually meant to replace having to use two functions separately. But, of course, you should use which ever you prefer.


Nov 28, 2012 at 3:22 PM // reply »
2 Comments

Hey I have a quick question for you regarding coldfusion date/time... pretty much I'm working on an employee portal and I want the timestamps for their posts to a bulletin board to be formatted to their timezone. I have their timezones saved according to their useraccounts (database) and now I'm trying to do the logic to do the math to display the correct time of the post in the database based on where in the world they're located. I keep getting the times an hour off. When the date/time stamp is saved to the database in a new post, I use the dateformat and timeformat. Are those two functions pulling the coldfusion server time or the computer's local clock time? If it's the server time, how do I figure out what the server time (or even timezone of the server) actually is using CF code? Thanks Ben! - Justin


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
Jun 19, 2013 at 2:01 PM
Experimenting With The Amazon Simple Storage Service (S3) API Using ColdFusion
I have coincidentally been beating my head against the S3 API for the last week or so. One big "gotcha" I had to work around was file names and paths containing spaces. Remember to URL Enco ... read »
Jun 19, 2013 at 1:27 PM
Using Slice(), Substring(), And Substr() In Javascript
very good article. By the way IE supports negative values in substr or slice in verson 10. ... read »
Jun 19, 2013 at 11:33 AM
Filter vs. ngHide With ngRepeat In AngularJS
In your assessment, is it correct to say that given a list of say 500 items its more performant to use the `ngHide` method over the `filter` method? ... read »
Jun 19, 2013 at 10:18 AM
ColdFusion Path Usage And Manipulation Overview
Anyone happen to know if the file created by getTempFile will be automatically removed at any point? Nothing mentioned in the docs, and restarting CF doesn't remove them, so it seems it needs manu ... read »
Jun 19, 2013 at 9:41 AM
Working With Inherited Collections In AngularJS
I actually just ran into this same situation with a demo I was putting together. Your implementation of multi-lvl $scope's > Mine :) ... read »
Jun 19, 2013 at 8:17 AM
My Experience With AngularJS - The Super-heroic JavaScript MVW Framework
@Prateek, to match a word or text you should use .toContain('word') that's a jasmine reference. website is : http://pivotal.github.io/jasmine/ ... read »
Jun 19, 2013 at 8:10 AM
My Experience With AngularJS - The Super-heroic JavaScript MVW Framework
Hi Guys, Actually i am doing e2e test of angular js of my project but i am not getting one thing that is how to press enter key through the test when my form is filled as i am not using a button but ... read »
Jun 18, 2013 at 9:20 PM
Mapping AngularJS Routes Onto URL Parameters And Client-Side Events
I couldn't find examples of passing multiple arguments using the when() routing statement so figured out through trial and error that you can pass multiple arguments using the following format: .whe ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools