Ryan Payne
Ryan Payne

Reputation: 6331

How do I convert a Calendar object to an ISO 8601 format DateTime string?

I'm trying to output the datePublished field in a Google AMP schema object. For example:

<script type="application/ld+json">
  {
      ...
      "datePublished": "2005-10-22T00:00Z",
      ...
  }
</script>

The datePublished field in my JCR app is rendering as a FreeMarker Calendar (e.g. Oct 21, 2005 7:00:00 PM) in the template.

How do I convert a data type of Calendar to an ISO 8601 format DateTime string (e.g. 2005-10-22T00:00Z)?

Upvotes: 1

Views: 193

Answers (1)

Ryan Payne
Ryan Payne

Reputation: 6331

Use the FreeMarker string.iso_m_u built-in. For example:

<script type="application/ld+json">
  {
      ...
      "datePublished": "${publishDate!?string.iso_m_u}",
      ...
  }
</script>

Reference: Built-ins for date/time/date-time values

Upvotes: 1

Related Questions