Skip to main content
Ben Nadel at RIA Unleashed (Nov. 2009) with: Andy Powell
Ben Nadel at RIA Unleashed (Nov. 2009) with: Andy Powell

ColdFusion DateTimeFormat() Utility Function

By
Published in Comments (14)

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.

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

Reader Comments

15,848 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.

357 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. ;)

15,848 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.

357 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.)

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.

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.

15,848 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.

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

20 Comments

A very old post this is, but for anyone that stumbles onto it as I just did...

I had created my own DateTimeFormat function (global function in CFWheels) in my last couple of projects (great minds think alike...).

We're in the process of upgrading to CF10 enterprise, and when running the code analyzer, along with a few other CFWheels-related issues, I was notified that my UDF now conflicts with the new internal DateTimeFormat() function within CF itself. Added in CF10... so now "I Gots Tuh Modifiez Muh Code."

1 Comments

Thanks for the work, and thanks to all the others who provided more links to follow. More information and examples are great things to have!

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