XML technologies / XSLT / Introduction to XSLT 2.0 / XSLT and XPath – round three / Date, duration and timestamp values / Date value with fn:current-date()

(Excerpt from "XSLT 2.0 & XPath 2.0" by Frank Bongers, Chapter 3.)

To obtain the current system date, simply call the function fn:current-date() — the value can, for example, be stored in a variable and used within a template rule:

<xsl:variable name="heute" select="fn:current-date()"/>

Code example: kap03/3.05/date.xsl (excerpt).

The function returns a date value of type xs:date. Output as a string, the return value (for 1 July 2007) looks as follows:

2007-07-01+01:00

This includes a time zone offset of +01:00; the date string itself has the form YYYY-MM-DD.

Formatting a date value for output

Usually, however, you do not want to use it in this form. The XSLT function format-date() allows the date string to be formatted as required for output. To obtain output in the form “01.07.2007”, pass the date value and a formatting string to the function:

<xsl:value-of select="format-date($heute,'[D01].[M01].[Y0001]')"/>

Code example: kap03/3.05/date.xsl (excerpt).

The value to be formatted is passed as the first parameter. The formatting string contains substrings in square brackets that serve as placeholders for the components to be extracted. Between them are literal characters passed through unchanged — here, separating periods.

   

back to top