Skip to content

Date and time

This utility provides date formatting functions. It was originally written by Allen Chang.

XML

To format a date, the entries in your data source should contain a node with a date field, for example:

              
<example-datasource>
  <entry id="152">
    ...
    <published-on iso="2025-08-22T17:45:00+02:00" timestamp="1755877500" time="17:45" weekday="5" offset="+0200">2025-08-22</published-on>
    ...
  </entry>
</example-datasource>

            

Call template

Add the template for date formatting to your main template, eg. <xsl:import href="utility-date-time.xsl"/>.

The named template format-date accepts two parameters:

  1. date - [required] ISO date (e.g. 2025-09-15)
  2. format - [optional] format string.

If you are inside a matching template for an entry, call the utility template like this:

              
<xsl:call-template name="format-date">
  <xsl:with-param name="date" select="published-on" />
  <xsl:with-param name="format" select="'M D, Y'" />
</xsl:call-template>

            

Format options

  • Y - year in 4 digits e.g. 1981, 1992, 2025
  • y - year in 2 digits e.g. 81, 92, 25
  • M - month as a full word e.g. January, March, September
  • m - month in 3 letters e.g. Jan, Mar, Sep
  • N - month in digits without leading zero
  • n - month in digits with leading zero
  • D - day with suffix and no leading zero e.g. 1st, 23rd
  • d - day in digits with leading zero e.g. 01, 09, 12, 25
  • x - day in digits with no leading zero e.g. 1, 9, 12, 25
  • T - time in 24-hours e.g. 18:30
  • t - time in 12-hours e.g. 6:30pm
  • W - weekday as a full word e.g. Monday, Tuesday
  • w - weekday in 3 letters e.g. Mon, Tue, Wed
  • Z - timezone (offset)

Possible errors

If “NaN” is displayed instead of the expected date, this usually has one of two causes:

  1. The entry does not contain an element with a date.
  2. There is a typing error for the node with the name that contains the date.