Thursday 28 January 2016

Use DDWRT to format your dates

The ddwrt date functions in SharePoint are not the easiest things to get to grips with. And it's quite difficult to find a definitive list of all the functions When I have found lists in other people's blogs, the functions posted don't always coincide with what I'm seeing in my site collection. This might be a result of us all using slightly different builds of SharePoint, but I'm going to list the ddwrt variations that I have observed on my SharePoint Site Collection by actually using them.

If you are going to use ddwrt date formatting in your Data View Web Part, you must include the line:

xmlns:ddwrt=http://schemas.microsoft.com/WebParts/v2/DataView/runtime

... inside the <xsl:stylesheet ...> tag, like this:

<xsl:stylesheet 
   version="1.0" exclude-result-prefixes="xsl msxsl ddwrt" 
   xmlns:x="http://www.w3.org/2001/XMLSchema" 
   xmlns:dsp="http://schemas.microsoft.com/sharepoint/dsp" 
   xmlns:asp="http://schemas.microsoft.com/ASPNET/20" 
   xmlns:__designer="http://schemas.microsoft.com/WebParts/v2/DataView/designer" 
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
   xmlns:msxsl="urn:schemas-microsoft-com:xslt" 
   xmlns:SharePoint="Microsoft.SharePoint.WebControls" 
   xmlns:ddwrt2="urn:frontpage:internal"
   xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime">

The 1033 LCID is used for US date formatting and the 2057 LCID is for UK date formatting. Where there's a hyphen in the Result column below, it means that the relevant ddwrt value produced no result for me.

DDWRT valueResult
<xsl:value-of select="ddwrt:FormatDate(string(@StartDate),1033,1)" />2/21/2016
<xsl:value-of select="ddwrt:FormatDate(string(@StartDate),1033,2)" />-
<xsl:value-of select="ddwrt:FormatDate(string(@StartDate),1033,3)" />Sunday, February 21, 2016
<xsl:value-of select="ddwrt:FormatDate(string(@StartDate),1033,4)" />1:00 PM
<xsl:value-of select="ddwrt:FormatDate(string(@EventDate),1033,5)" />2/21/2016 1:00 PM
<xsl:value-of select="ddwrt:FormatDate(string(@EventDate),1033,6)" />1:00 PM
<xsl:value-of select="ddwrt:FormatDate(string(@EventDate),1033,7)" />Sunday, February 21, 2016 1:00 PM
<xsl:value-of select="ddwrt:FormatDate(string(@EventDate),1033,8)" />-
<xsl:value-of select="ddwrt:FormatDate(string(@EventDate),1033,9)" />2/21/2016
<xsl:value-of select="ddwrt:FormatDate(string(@EventDate),1033,10)" />-
<xsl:value-of select="ddwrt:FormatDate(string(@EventDate),1033,11)" />Sunday, February 21, 2016
<xsl:value-of select="ddwrt:FormatDate(string(@EventDate),1033,12)" />1:00:00 PM
<xsl:value-of select="ddwrt:FormatDate(string(@EventDate),1033,13)" />2/21/2016 1:00:00 PM
<xsl:value-of select="ddwrt:FormatDate(string(@EventDate),1033,14)" />1:00 PM
<xsl:value-of select="ddwrt:FormatDate(string(@EventDate),1033,15)" />Sunday, February 21, 2016 1:00:00 PM
<xsl:value-of select="ddwrt:FormatDate(string(@EventDate),1033,16)" />-
<xsl:value-of select="ddwrt:FormatDate(string(@EventDate),1033,17)" />2/21/2016
<xsl:value-of select="ddwrt:FormatDate(string(@EventDate),1033,18)" />-
<xsl:value-of select="ddwrt:FormatDate(string(@EventDate),1033,19)" />Sunday, February 21, 2016
<xsl:value-of select="ddwrt:FormatDate(string(@EventDate),1033,20)" />1:00 PM
<xsl:value-of select="ddwrt:FormatDate(string(@EventDate),1033,21)" />2/21/2016 1:00 PM
<xsl:value-of select="ddwrt:FormatDate(string(@EventDate),1033,22)" />1:00 PM
<xsl:value-of select="ddwrt:FormatDate(string(@EventDate),1033,23)" />Sunday, February 21, 2016 1:00 PM
<xsl:value-of select="ddwrt:FormatDate(string(@StartDate),1033,25)" />2/21/2016
<xsl:value-of select="ddwrt:FormatDate(string(@StartDate),1033,27)" />Sunday, February 21, 2016
<xsl:value-of select="ddwrt:FormatDate(string(@EventDate),1033,28)" />1:00:00 PM
<xsl:value-of select="ddwrt:FormatDate(string(@EventDate),1033,29)" />2/21/2016 1:00:00 PM
<xsl:value-of select="ddwrt:FormatDate(string(@EventDate),1033,30)" />1:00:00 PM
DDWRT valueResult
<xsl:value-of select="ddwrt:FormatDate(string(@StartDate),2057,1)" />21/02/2016
<xsl:value-of select="ddwrt:FormatDate(string(@StartDate),2057,2)" />-
<xsl:value-of select="ddwrt:FormatDate(string(@StartDate),2057,3)" />21 February 2016
<xsl:value-of select="ddwrt:FormatDate(string(@StartDate),2057,4)" />13:00
<xsl:value-of select="ddwrt:FormatDate(string(@EventDate),2057,5)" />21/02/2016 13:00
<xsl:value-of select="ddwrt:FormatDate(string(@EventDate),2057,6)" />13:00
<xsl:value-of select="ddwrt:FormatDate(string(@EventDate),2057,7)" />21 February 2016 13:00
<xsl:value-of select="ddwrt:FormatDate(string(@EventDate),2057,8)" />-
<xsl:value-of select="ddwrt:FormatDate(string(@EventDate),2057,9)" />21/02/2016
<xsl:value-of select="ddwrt:FormatDate(string(@EventDate),2057,10)" />-
<xsl:value-of select="ddwrt:FormatDate(string(@EventDate),2057,11)" />21 February 2016
<xsl:value-of select="ddwrt:FormatDate(string(@EventDate),2057,12)" />13:00:00
<xsl:value-of select="ddwrt:FormatDate(string(@EventDate),2057,13)" />21/02/2016 13:00:00
<xsl:value-of select="ddwrt:FormatDate(string(@EventDate),2057,14)" />13:00:00
<xsl:value-of select="ddwrt:FormatDate(string(@EventDate),2057,15)" />21 February 2016 13:00:00
<xsl:value-of select="ddwrt:FormatDate(string(@EventDate),2057,16)" />-
<xsl:value-of select="ddwrt:FormatDate(string(@EventDate),2057,17)" />21/02/2016
<xsl:value-of select="ddwrt:FormatDate(string(@EventDate),2057,18)" />-
<xsl:value-of select="ddwrt:FormatDate(string(@EventDate),2057,19)" />21 February 2016
<xsl:value-of select="ddwrt:FormatDate(string(@EventDate),2057,20)" />13:00
<xsl:value-of select="ddwrt:FormatDate(string(@EventDate),2057,21)" />21/02/2016 13:00
<xsl:value-of select="ddwrt:FormatDate(string(@EventDate),2057,22)" />13:00
<xsl:value-of select="ddwrt:FormatDate(string(@EventDate),2057,23)" />21 February 2016 13:00
<xsl:value-of select="ddwrt:FormatDate(string(@StartDate),2057,25)" />21/02/2016
<xsl:value-of select="ddwrt:FormatDate(string(@StartDate),2057,27)" />21 February 2016
<xsl:value-of select="ddwrt:FormatDate(string(@EventDate),2057,28)" />13:00:00
<xsl:value-of select="ddwrt:FormatDate(string(@EventDate),2057,29)" />21/02/2016 13:00:00
<xsl:value-of select="ddwrt:FormatDate(string(@EventDate),2057,30)" />13:00:00

If you're using the FormatDateTime specifier, it doesn't matter whether you define the LCID as 1033 or 2057.

DDWRT valueResult
<xsl:value-of select="ddwrt:FormatDateTime(string(@EventDate),1033,'dd-MMM-yy')" />21-Jan-15
<xsl:value-of select="ddwrt:FormatDateTime(string(@EventDate),1033,'dd/MM/yy')" />21/01/15
<xsl:value-of select="ddwrt:FormatDateTime(string(@EventDate),1033,'dd/M/yy')" />21/1/15
<xsl:value-of select="ddwrt:FormatDateTime(string(@EventDate),1033,'ddd, dd/M/yy')" />Sun, 21/1/15
<xsl:value-of select="ddwrt:FormatDateTime(string(@EventDate),1033,'dddd, dd/M/yy')" />Sunday, 21/1/15

The full list of specifiers is as follows:

SpecifierResult
dDay of the month, single digit 1-9, double digit 10 up
ddDay of the month, leading zero 1-9, double digit 10 up
dddAbbreviated day of the week, ie, Wed
ddddFull name of the day of the week
Specifier Result
MThe month, 1 to 12
MMThe month, 01 to 12
MMMAbbreviated name of the month
MMMMFull name of the month
Specifier Result
yThe year, from 0 to 99
yyThe year, from 00 to 99
yyyThe year with a minumum of three digits, ie 900
yyyyThe year as a four-digit number

You can also add the following, if you want to display the time, as well:

Specifier Result
h12-hour clock, 1 to 12
hh12-hour clock, 01 to 12
H24 hour clock, 1 to 24
HH24 hour clock, 01 to 24
mMinutes, 0 to 59
mmMinutes, 00 to 59
sSeconds, 0 to 59
ssSeconds, 00 to 59

Use anything you like to separate the components ... a dash, a slash or wordpsaces and/or commas.

Hope this helps someone.


4 comments:

  1. I think you forgot the minutes specifiers in the last table. I am using the following: yyyy-MM-dd hh:mm, 2017-07-24 09:20.

    ReplyDelete
  2. You don't need to call string '<xsl:value-of select="ddwrt:FormatDate(string(@EventDate),2057,30)" />'.
    This is enough: '<xsl:value-of select="ddwrt:FormatDate(@EventDate,2057,30)" />'

    ReplyDelete