[2.0] Formatting Dates and Times

XSLT 2.0 adds three new formatting functions, format-date(), format-time(), and format-dateTime(). We’ll take xs:date, xs:time, and xs:dateTime values and format them in some useful way.

You can call each of these functions in two ways. The simplest is to pass the function a value and a formatting string. If you need more detail, the second way of calling these functions requires you to specify a language, a calendar, and a country as well. The XSLT 2.0 specification lists more than 25 different calendars used around the world, and there are hundreds of combinations of language and country codes. Look at the documentation for your XSLT processor to see which calendars, languages, and countries it supports.

Our first example is pretty simple. We’ll create a stylesheet that uses the XPath functions current-date(), current-time(), and current-dateTime():

<?xml version="1.0" encoding="utf-8"?>
<!-- datetime1.xsl -->
<xsl:stylesheet version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output method="text"/>

  <xsl:template match="/">
    <xsl:text>&#xA;Tests of date and time formatting:&#xA;</xsl:text>
    <xsl:text>&#xA;  The current date is </xsl:text>
    <xsl:value-of select="format-date(current-date(), 
                          '[M01]/[D01]/[Y0001]')"/>
    <xsl:text>&#xA;  The current time is </xsl:text>
    <xsl:value-of select="format-time(current-time(), 
                          '[H01]:[m01] [z]')"/>
    <xsl:text>&#xA;  It's currently </xsl:text>
    <xsl:value-of select="format-dateTime(current-dateTime(),
                          '[h1]:[m01] [P] on [MNn] [D].')"/>
  </xsl:template>

</xsl:stylesheet>

This stylesheet produces this text:

Tests of date and time formatting:

  The current date is 03/08/2006
  The current time is 22:27 GMT-5
  It's currently 10:27 p.m. on March 8.

In this stylesheet, M01 produces the two-digit month, D01 produces the two-digit day, and Y0001 produces the four-digit year. H01 produces the 2-digit hour in a 24-hour clock, m01 produces the 2-digit minutes, z produces the time zone, h1 produces the 1- or 2-digit hour in a 12-hour clock, and P generates a.m. or p.m. Finally, MNn generates the capitalized name of the month.

The formatting codes used by the <xsl:number> element can be used in the picture string for these functions. Here’s another stylesheet that uses more of those formatting codes:

<?xml version="1.0" encoding="utf-8"?>
<!-- datetime2.xsl -->
<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="text"/>

  <xsl:template match="/">
    <xsl:text>&#xA;More tests of date and time formatting:&#xA;</xsl:text>
    <xsl:text>&#xA;  Today is the </xsl:text>
    <xsl:value-of select="format-date(current-date(), 
                          '[Dwo] day of [MNn], [Y0001]')"/>
    <xsl:text>&#xA;  Right now is the </xsl:text>
    <xsl:value-of select="format-time(current-time(), 
                          '[m1o] minute of the [Hwo] hour of the day.')"/>
    <xsl:text>&#xA;  It's currently </xsl:text>
    <xsl:value-of select="format-dateTime(current-dateTime(),
                          '[h01]:[m01] [P] on [FNn] the [D1o].')"/>
    <xsl:text>&#xA;  Today is the </xsl:text>
    <xsl:value-of select="format-date(current-date(),
                          '[dwo]')"/>
    <xsl:text> day of the year. </xsl:text>
    <xsl:text>&#xA;  December 25, 1960 in German: </xsl:text>
    <xsl:value-of select="format-date(xs:date('1960-12-25'), 
                          '[D] [MNn,3-3] [Y0001]', 'de', 
                          'AD', 'DE')"/>
  </xsl:template>

</xsl:stylesheet>

This stylesheet generates this text:

More tests of date and time formatting:

  Today is the eighth day of March, 2006
  Right now is the 28th minute of the twenty-second hour of the day.
  It's currently 10:28 p.m. on Wednesday the 8th.
  Today is the sixty-seventh day of the year. 
  December 25, 1960 in German: 25 Dez 1960

Here are the explanations for all the formatting codes in this example:

Dwo

The word for the ordinal value of the day

MNn

The capitalized name of the month

Y0001

The four-digit year

m1o

The numeric ordinal of the minute

Hwo

The hour, expressed as an ordinal word

h01

The 2-digit hour in a 24-hour clock

m01

The 2-digit minute

P

The a.m. or p.m. indicator

FNn

The capitalized name of the day of the week

D1o

The numeric ordinal of the day

dwo

The day of the year, expressed as an ordinal word

D

The numeric day of the month

MNn,3-3

The capitalized name of the month, returned in a string that’s at least three characters long, but no more than three characters long

For the last call to format-date(), we used the five-option version of the function, passing in the language, calendar and country codes. The formatting codes defined by XSLT give you a wide range of options for formatting the different components of dates and times.

Get XSLT, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.