Formatting Dates & Times in XSLT 2.0

The CorasWorks Application Service brings the power of XSLT 2.0 to SharePoint 2010 & 2013 – and with it, the myriad of new functions and capabilities the 2.0 spec includes. A common need, especially when working with SharePoint data, is to reformat Date and Time values as SharePoint returns them in the format yyyy-MM-dd hh:mm:ss (or sometimes even yyyy-MM-ddThh:mm:ssZ). Let’s deconstruct this real quick, since knowing your input will ensure you apply the right options to get the desired output.

Let’s reference the date CAPS was released – July 15, 2013 at midnight – for all the below:

Format String Description Result
yyyy 4-digit year 2013
MM 2-digit month 07
dd 2-digit day 15
hh 2-digit hour (12-hour format) 12
mm 2-digit minute 00
ss 2-digit second 00

With XSLT 2.0, not only is Date & Time formatting now supported natively, but the options you can specify are extremely flexible, including multilingual support (dependent on support being enabled in your XSL processor).

Here are some of the most common use-cases and how to implement them:

Format String Description Result
format-date($Date,
‘[M]/[D]/[Y]‘)
Default US format 7/15/2013
format-date($Date,
‘[M01]/[D01]/[Y0001]‘)
2-digit month, slash,
2-digit day, slash,
2-digit year
07/15/13
format-date($Date,
‘[D1o] [MNn], [YWw]‘)
Ordinal day month name,
year as words
15th July,
Two Thousand and Thirteen
format-date($Date,
‘The [Dwo] day of [MNn], [Y]‘)
Date as a word, name of month, year The fifteenth day of July, 2013
format-date($Date,
‘[FNn], [MNn] [D1o], [Y]‘)
Day of week, month name, year Monday, July 15th, 2013
format-date($Date,
‘[MNn] [D1o] is the [dwo] day of [Y]‘)
Day of year July 15th is the
one hundred and ninety-sixth day of 2013
format-date($Date,
‘[FNn,3-3]., [MNn] [D1o], [Y]‘)
Day of week abbrev., month, day ordinal,
year
Mon., July 15th, 2013
format-date($Date,
‘[MNn,3-3]. [D1o], [Y]‘)
Abbreviated month, day ordinal, year Jul. 15th, 2013
format-time($Time,
‘[H]:[m] is the [m1o] minute of the [Hwo] hour’)
Minute ordinal and
hour ordinal
8:30 is the 30th minute of the eighth hour
format-time($Time,
‘[h02]:[m] – or [h02][m] in military format’)
Time in 24hr clock, or in military format 08:30 – or 0830 in military format
format-time($Time,
‘[H]:[m] can be clarified as [H]:[m] [P]‘)
Time with AM/PM lowercase 8:30 can be clarified as 8:30 a.m.
format-time($Time,
‘[H]:[m] can be clarified as [H]:[m] [PN]‘)
Time with AM/PM uppercase 8:30 can be clarified as 8:30 A.M.
format-time($Time,
‘[H]:[m] can be clarified as [H]:[m][PN,2-2]‘)
Time with AM/PM uppercase and no periods 8:30 can be clarified as 8:30AM

Hopefully these cover all the common usage scenarios and you can take advantage of the greatly improved Date & Time formatting in XSLT 2.0. For more info, check out the detailed spec on the W3 site here.

Comments are closed.