Sponsored

XSLT Reference

format-dateTime()

XSLT 2.0 date function

Formats an xs:dateTime value into a human-readable string using a picture pattern, with optional locale and calendar support.

Syntax
format-dateTime(dateTime, picture, language?, calendar?, place?)

Description

format-dateTime() converts an xs:dateTime value into a formatted string using the picture pattern syntax shared by format-date() and format-time(). It supports all date and time component specifiers in a single call.

Common specifiers:

SpecifierMeaning
[Y]Year (4 digits by default)
[M]Month as a number
[MNn]Month name (e.g., “April”)
[D]Day of the month
[H]Hour, 24-hour clock (0–23)
[h]Hour, 12-hour clock (1–12)
[m]Minute
[s]Second
[P]AM/PM marker
[Z]Timezone offset

Width modifiers (e.g., [D01], [m01]) control zero-padding.

Parameters

ParameterTypeRequiredDescription
dateTimexs:dateTime?YesThe dateTime value to format. Returns an empty string for the empty sequence.
picturexs:stringYesThe picture pattern controlling the output format.
languagexs:string?NoBCP 47 language tag (e.g., "en", "fr").
calendarxs:string?NoCalendar system identifier. Implementation-defined.
placexs:string?NoPlace or timezone identifier. Implementation-defined.

Return value

xs:string — the formatted date-time string, or an empty string if dateTime is the empty sequence.

Examples

Format an event timestamp

Input XML:

<?xml version="1.0" encoding="UTF-8"?>
<events>
  <event timestamp="2026-04-18T09:30:00">Morning session</event>
  <event timestamp="2026-04-18T14:00:00">Afternoon workshop</event>
</events>

Stylesheet:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="/events">
    <events>
      <xsl:for-each select="event">
        <event display="{format-dateTime(xs:dateTime(@timestamp), '[D] [MNn] [Y] at [H01]:[m01]')}">
          <xsl:value-of select="."/>
        </event>
      </xsl:for-each>
    </events>
  </xsl:template>
</xsl:stylesheet>

Output:

<events>
  <event display="18 April 2026 at 09:30">Morning session</event>
  <event display="18 April 2026 at 14:00">Afternoon workshop</event>
</events>

Embed a generation timestamp in a report

Stylesheet:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="/">
    <report generated="{format-dateTime(current-dateTime(), '[Y]-[M01]-[D01]T[H01]:[m01]:[s01]')}">
      <xsl:apply-templates/>
    </report>
  </xsl:template>
</xsl:stylesheet>

Output (example):

<report generated="2026-04-18T14:32:07">
  ...
</report>

Notes

  • The input must be xs:dateTime, not a plain string. Cast attribute values with xs:dateTime(@attr).
  • Picture syntax follows the XPath 2.0 specification, not Java or POSIX conventions.
  • To format only the date or time portion of a xs:dateTime, cast first: format-date(xs:date(current-dateTime()), ...).

See also