A handy addition in XSLT 2.0 is a nice set of date and time format XPath functions. XSLT 1.0 was decidedly lacking in these functions, so you may have used a third-party library such as EXSLT for this purpose. EXSLT offers arithmetic functions on dates and times (still absent in XSLT 2.0) but the XSLT 2.0 offering is perfect for formatting dates and times as strings. It also means that you don't have to worry about importing the third-party library.
The XSLT 2.0 Specification specifies three functions for formatting dates:
- format-dateTime() for a combined date and time
- format-date() for a date
- format-time() for a time
Each of these functions has two variants. The first simply takes the date/time as the first argument, and a Picture as the second. The second variant is a little more complex. As well as taking the date/time and the Picture, it also requires a language. a calendar and a country.
A picture string is simple a date format string. The following pictures are available:
|Default Presentation Modifier
|year (absolute value)
|month in year
|day in month
|day in year
|day of week
|week in year
|week in month
|hour in day (24 hours)
|hour in half-day (12 hours)
|minute in hour
|second in minute
|timezone as a time offset from UTC, or if an alphabetic modifier is present the conventional name of a timezone (such as PST)
|timezone as a time offset using GMT, for example GMT+1
|calendar: the name or abbreviation of a calendar name
|era: the name of a baseline for the numbering of years, for example the reign of a monarch
Each picture that results in a number must be coupled with a number format token.
|1 2 3 ... 100 ...
|A B C .... Z AA AB AC ...
|a b c ... z aa ab ac ...
|i ii iii iv v vi vii .. x .. m ..
|I II III IV V VI VII .....
|one two three four ...
|ONE TWO THREE FOUR ...
|One Two Three Four ...
|first second third ...
Often you will want to pad out some numbers with zeroes to a fixed length. This is performed by prefixing the Format Token with zeroes up to the length required. For example, a format token that would pad the day with zeroes up to four digits would be [D0001].
The below XSLT has been tested against Saxon EE. (Foreign language support is not included with the open source Home Edition)
The resulting output HTML is as follows: