ColdFusion Zeus POTW - Extra edition...

This post is more than 2 years old.

Ok, I know I did a Zeus POTW earlier this week, but I had to share this little gem. This is another small thing - a simple function - and something that's been possible via a CFLib UDF for years. That being said - when I saw this show up in the latest Zeus build today, I had to share.

If you've ever needed to format a date and a time, you know that it requires two calls, ala:

<cfoutput> #dateFormat(now())# #timeFormat(now())# </cfoutput>

Zeus adds dateTimeFormat to combine this into one call:

<cfoutput> #dateTimeFormat(now())#<br/> </cfoutput>

For formatting, the mask is slightly different than before. Masks are based on SimpleDateFormat from Java. So for example:

<cfoutput> #dateTimeFormat(now(), "MMMM d, yyyy h:mm a")#<br/> </cfoutput>

Yeah, ok, not Earth-shattering, but this was one of a few new functions released in the latest Zeus build that were things I've seen requested for years.

Raymond Camden's Picture

About Raymond Camden

Raymond is a senior developer evangelist for Adobe. He focuses on document services, JavaScript, and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA

Archived Comments

Comment 1 by O?uz Demirkap? posted on 12/21/2011 at 9:16 AM

LSdateTimeFormat? :)

Comment 2 by Raymond Camden posted on 12/21/2011 at 9:33 AM

I didn't show it - but there is a 3rd arg - locale:

#dateTimeFormat(now(), "MMMM d, yyyy h:mm a","fr")#

Outputs: d├ęcembre 20, 2011 10:33 PM

Comment 3 by Atan posted on 12/21/2011 at 2:16 PM

> For formatting, the mask is slightly different than before.

You mean that I can't combine the variables dateFmt="yyyy/mm/dd" and timeFmt="HH:mm" (the values are internationalized for each installation) I already have? That would be a bad idea. CF should be compatible to itself.

Comment 4 by James Harris posted on 12/21/2011 at 5:48 PM

@Atan -

I think what he's saying is that the mask format is different only for the new function. Coldfusion would need a way to discern date vs. time placeholders. If you look, the only ones that are actually different are the placeholders that are shared between both date and time masks.

That being said, your HH:mm would still produce a perfectly valid military time formatted time string. However, If I am taking this in properly, "yyyy/mm/dd" would actually generate "2011/[current minutes]/21" It would need to be "yyyy/MM/dd" for use in this new formatting function.

I could be wrong on all this, though. Just a thought.

Confirmation Ray?

Comment 5 by Raymond Camden posted on 12/21/2011 at 6:20 PM

Not sure I can confirm this as I wasn't involved in the engineering, but it sounds right to me. ;)

Comment 6 by Az posted on 12/21/2011 at 8:29 PM

Since this is just a string value that tells the function how to format the output they should make the older functions for time and date formatting recognize the newer versions of the masks. The masks for the combined date and time format function need to be recognized by the older functions so you can set global formatting variables for date and time. I think this is what Atan meant by his post. The old functions should still recognize the old versions of the masks for backward compatibility, however, having the new masks supported is something I see as a "no brainer" for the development team to implement and should not be overlooked.


Comment 7 by Fred posted on 12/21/2011 at 9:51 PM

Finally! This is nice :-)

Comment 8 by Joseph Potthast posted on 12/22/2011 at 5:49 AM

You're right in that it's not earth shateringly big of a change that I'm going to want to go back to old projects and update but you know what? It's useful and easy so I see no reason not to use it in the future ones.

Comment 9 by Prabesh Poudyal posted on 12/23/2011 at 1:34 AM

Like it, I so want to see this being used more aggressively and staring to support different international calendars. That would be amazing. When you are looking at an international page, Google Translate can only do so much....