Thursday, 21 February 2008

Extract the Month from a DateTime Field in Sitecore

Using sc:formatdate(), it is a fairly trivial matter to format a date in a Sitecore Xslt rendering. Eg:
sc:formatdate(sc:fld('MyDateField', .),
'd MMM yyyy @ h:mm')

However, extracting just the month as a number without padding is less obvious. Using the format string 'MMM' gives three-letter month names, 'MM' gives months padded with zeros (eg. 02 for Feb), while 'M' curiously gives full month name and date padded with zeros.

A workaround is to use substring-after in conjunction with the format '/M'. Example:
sc:fld('MyDateField', .), '/M'), '/')

1 comment:

LarsNielsen said...

Hey Niel,

You can do the same, from within XSLT using the date custom sc xsl control, e.g.:
< sc:date format="d MMM yyy @ h:mm" />

The formats are traditional .NET formats: