<p>Try the Twig <a href="http://twig.sensiolabs.org/doc/filters/date.html" rel="nofollow"><code>date</code></a> filter instead:</p>

<pre><code>{{ article.postDate|date('jS F Y') }}
</code></pre>

<p>to wrap the ordinal in <code>&lt;sup&gt;</code>:</p>

<pre><code>{{ article.postDate|date('j') }}&lt;sup&gt;{{ article.postDate|date('S') }}&lt;/sup&gt;
{{ article.postDate|date('F Y') }}
</code></pre>

<p>if you think it's to ugly for use in your templates, consider "hiding" it in <a href="http://twig.sensiolabs.org/doc/tags/macro.html" rel="nofollow">a macro</a>.</p>

CC BY-SA 3.0
2016-01-16T16:34:29.073Z
I tried that as well, but I'm looking to put a `sup` element around the ordinal suffix. I'd do this using the date function by escaping the characters.
William Isted
CC BY-SA 3.0
2016-01-16T16:32:32.740Z
2016-01-16T16:39:14.663Z
2016-01-16T16:39:14.663Z
carlcs
<p>If you're using Craft 3, the <a href="https://github.com/nystudio107/craft3-typogrify" rel="nofollow noreferrer">Typogrify</a> plugin will do the <code>&lt;sup&gt;</code> on ordinal numbers automatically for you.</p>

CC BY-SA 3.0
2017-11-11T15:08:51.170Z
2017-11-11T15:08:51.170Z
andrew.welch
<p>After trial and error I figured out you can escape characters using double backslash. </p>

<pre><code>{{ article.postDate.format('j\\&lt;\\s\\u\\p\\&gt;S\\&lt;\\/\\s\\u\\p\\&gt; F Y') | raw }}
</code></pre>

<p>Outputs: <code>14&lt;sup&gt;th&lt;/sup&gt; January 2016</code></p>

<p>In search of documentation I found a mention of <a href="https://craftcms.stackexchange.com/a/4271/4118">double-escaping regular expressions</a> due to Twig auto-escaping <code>\</code>. At this point I'm guessing this works for most Craft-Twig functions.</p>

CC BY-SA 3.0
2016-01-17T01:09:11.493Z
2016-07-04T23:00:02.737Z
2017-04-13T12:51:00.200Z
William Isted
<p>I'm trying to find a method of escaping characters in Craft's <code>DateTime</code> object.</p>

<p>This is the output I'm trying to achieve: <code>16th January 2016</code></p>

<p>This is what I've tried: <code>{{ article.postDate.format('j\&lt;\s\u\p\&gt;S\&lt;\/\s\u\p\&gt; F Y') }}</code></p>

<p>This is the output: <code>16&amp;lt;00000000p&amp;gt;th&lt;!--00000000p--&gt; January 2016</code></p>

<p>I can achieve the output by appending output together but there <strong>has</strong> to be a better way of achieving this than the following:</p>

<p><code>{{ article.postDate.format('j') }}&lt;sup&gt;{{ article.postDate.format('S') }}&lt;/sup&gt; {{ article.postDate.format(' F Y') }}</code></p>

<p>Any pointers to documentation would be appreciated.</p>

<templating><datetime><escaping>
Escape characters in format
# Escape characters in format

(Asked by: username_0 on 2016-01-16)

I'm trying to find a method of escaping characters in Craft's `DateTime` object.

This is the output I'm trying to achieve: `16th January 2016`

This is what I've tried: `{{ article.postDate.format('j\<\s\u\p\>S\<\/\s\u\p\> F Y') }}`

This is the output: `16<00000000p>th January 2016`

I can achieve the output by appending output together but there **has** to be a better way of achieving this than the following:

`{{ article.postDate.format('j') }}{{ article.postDate.format('S') }} {{ article.postDate.format(' F Y') }}`

Any pointers to documentation would be appreciated.

**Tags:** templating, datetime, escaping

---

## 3 answers

### Answer by username_0 on 2016-01-17. Score: 5 | Accepted answer

After trial and error I figured out you can escape characters using double backslash. 
{{ article.postDate.format('j\\<\\s\\u\\p\\>S\\<\\/\\s\\u\\p\\> F Y') | raw }}

Outputs: 14th January 2016
In search of documentation I found a mention of double-escaping regular expressions due to Twig auto-escaping \. At this point I'm guessing this works for most Craft-Twig functions.


---

### Answer by username_1 on 2016-01-16. Score: 3

Try the Twig date filter instead:
{{ article.postDate|date('jS F Y') }}

to wrap the ordinal in :
{{ article.postDate|date('j') }}{{ article.postDate|date('S') }}
{{ article.postDate|date('F Y') }}

if you think it's to ugly for use in your templates, consider "hiding" it in a macro.

> **Comments:**
> **username_0**: I tried that as well, but I'm looking to put a `sup` element around the ordinal suffix. I'd do this using the date function by escaping the characters.  

---

### Answer by username_2 on 2017-11-11. Score: 1

If you're using Craft 3, the [Typogrify](https://github.com/nystudio107/craft3-typogrify) plugin will do the  on ordinal numbers automatically for you.


---

