December 9, 2018
Quick tip: escaping string literals in dateTimeFormat()
Comments
(4)
December 9, 2018
Quick tip: escaping string literals in dateTimeFormat()
Newbie 49 posts
Followers: 42 people
(4)

Just a quick tip since I didn’t find this to be well documented anywhere and someone else probably has this exact same problem.  Frequently, I display time and date on a page.  I used to do it like so:

#dateFormat(now(),'dddd, mmmm d, yyyy')# at #timeFormat(now(),'h:mm tt')#

…but why use two functions when you can use one, right?  So I tried using the following:

#dateTimeFormat(now(),'dddd, mmmm d, yyyy at h:mm tt')#

But got briefly stuck when the ‘t’ in the date mask was getting translated to “apm” instead of the literal “at”.  I said to myself, “Self… you need to escape those literal characters.”  I came up with this:

#dateTimeFormat(now(),'dddd, mmmm d, yyyy "at" h:mm tt')#

But received the same result.  As it turns out, in this case, double quotes and single quotes are important.  The quick tip here is: escaping literal characters in a dateTimeFormat() mask requires the use of double quotes outside the mask and single quotes to escape your literal characters.

The winner was the following:

#dateTimeFormat(now(),"dddd, mmmm d, yyyy 'at' h:mm tt")#
4 Comments
2018-12-18 04:34:56
2018-12-18 04:34:56

Hi David,

Nice find! Filed https://tracker.adobe.com/#/view/CF-4203797 giving you credit.

Thanks!,
-Aaron

Like
(1)
2018-12-10 18:02:28
2018-12-10 18:02:28

This also works (single quotes around the date arguments, double single quotes around AT):
#dateTimeFormat(now(),’dddd, mmmm d, yyyy ”at” h:mm tt’)#

Like
(1)
(1)
>
Mark.Gregory
's comment
2018-12-10 20:20:26
2018-12-10 20:20:26
>
Mark.Gregory
's comment

Mark;  This does not work under ACF at least.

CFFiddle example

Like
2018-12-10 16:26:53
2018-12-10 16:26:53

I hope Adobe adds this to their documentation.
Can you add a link to a live version.

Like
Add Comment