Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-01-16T16:34:29.073Z
Answers.Text: 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.
Answers.UserDisplayName: William Isted
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-01-16T16:32:32.740Z
Answers.LastActivityDate: 2016-01-16T16:39:14.663Z
Answers.LastEditDate: 2016-01-16T16:39:14.663Z
Answers.UserDisplayName: carlcs
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-11-11T15:08:51.170Z
Answers.LastActivityDate: 2017-11-11T15:08:51.170Z
Answers.UserDisplayName: andrew.welch
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-01-17T01:09:11.493Z
Answers.LastActivityDate: 2016-07-04T23:00:02.737Z
Answers.LastEditDate: 2017-04-13T12:51:00.200Z
Answers.UserDisplayName: William Isted
Body: <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>
  
Tags: <templating><datetime><escaping>
Title: Escape characters in format
ThreadText: # 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&lt;00000000p&gt;th<!--00000000p--> 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') }}<sup>{{ article.postDate.format('S') }}</sup> {{ 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: `14<sup>th</sup> January 2016`
  
  In search of documentation I found a mention of [double-escaping regular expressions](https://craftcms.stackexchange.com/a/4271/4118) 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`](http://twig.sensiolabs.org/doc/filters/date.html) filter instead:
  
  ```
  {{ article.postDate|date('jS F Y') }}
  
  ```
  
  to wrap the ordinal in `<sup>`:
  
  ```
  {{ article.postDate|date('j') }}<sup>{{ article.postDate|date('S') }}</sup>
  {{ article.postDate|date('F Y') }}
  
  ```
  
  if you think it's to ugly for use in your templates, consider "hiding" it in [a macro](http://twig.sensiolabs.org/doc/tags/macro.html).
  
  > **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 `<sup>` on ordinal numbers automatically for you.
  
  
  ---
  
  