Skip to main content
Ben Nadel at Scotch On The Rock (SOTR) 2010 (London) with: David Phipps
Ben Nadel at Scotch On The Rock (SOTR) 2010 (London) with: David Phipps ( @phipps73 )

Using String Literals In Date / Time Formatting In Lucee CFML

By on

A few weeks, I took a look at the formatDate() function in Angular 10. In that post, I demonstrated that the date mask used by the formatDate() function could include escaped string literals that would be output as-is without being interpolated. After that post, I wondered if the same thing could be done in Lucee CFML. It doesn't look like it works in dateFormat() or timeFormat(). But then, I stumbled upon this Adobe post by David Byers. In it, he demonstrated that string literals do work in the dateTimeFormat() function. I rarely use the dateTimeFormat() function, so I wanted to try this for myself in Lucee CFML

According to David's article, using a string literal in the dateTimeFormat() function works just like it does within the formatDate() function in Angular 10. That is, all you have to do is surround your string literal in single-quotes. Let's try that:


		now().dateFormat( "yyyy-mm-dd 'and a string literal'" )
		now().timeFormat( "HH:nn:ss TT 'and a string literal'" )
		now().dateTimeFormat( "yyyy-mm-dd HH:nn:ss TT 'and a string literal'" )

	// ------------------------------------------------------------------------------- //
	// ------------------------------------------------------------------------------- //

	public void function echoLine(
		required string key,
		required string value
		) {

		echo( "<p> <strong>#key# &rarr;</strong> #value# </p>" );



As you can see, I am testing this in the three main date formatting functions, each of which contains the phrase, 'and a string literal'. And, if we run this ColdFusion code in Lucee CFML, we get the following output:

Showing the output of string literals in dateFormat(), timeFormat(), and dateTimeFormat() in Lucee CFML.

As you can see, both the dateFormat() and timeFormat() functions consumed the string literal as part of the mask. However, the dateTimeFormat() function was able to treat the string literal as a stand-alone value, keeping it in the output as-is (but removing the single-quote).

It seems odd that this works in one date/time function but not the others. I'll have to check to see if there is a feature-enhancement request somewhere in the Lucee Dev Tickets. That said, it's pretty cool that string literals do work in dateTimeFormat(). I don't often need them; but, now I have this hot tip in my back-pockets.

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

Reader Comments

What has two thumbs and hopes you leave a comment? This Guy! (Ben Nadel).

Post A Comment — I'd Love To Hear From You!

NEW: Some basic markdown formatting is now supported: bold, italic, blockquotes, lists, fenced code-blocks. Read more about markdown syntax »
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.
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