Showing posts with label Date Format. Show all posts
Showing posts with label Date Format. Show all posts

Tuesday, January 26, 2016

Custom Date Formats in SharePoint XSL

Custom Date Formats in SharePoint XSL

There are quite a few posts out there on this topic, but I’m yet to find one comprehensive post that walks through this beginning to end and actually works.  Let’s give it a go.

A very common scenario for SharePoint publishing sites is to customize the look to suit the customers needs.  Usually this is done with a Content Query Web Part and some custom XSL.  When doing this very often you need to display a date.  You will quickly notice that just displaying the date that SharePoint gives you is not going to be sufficient.  If you just did the standard

<xsl:value-of select="@ArticleStartDate"/>
You get back a pretty nasty looking result

2009-03-23 00:00:00



However if you use the “FormatDate” function, you can make this look a lot better.

<xsl:value-of select="ddwrt:FormatDate(@ArticleStartDate, 2057, 3)"/>

Results in this

23 March 2009

All you need to do to make sure the “FormatDate” function is available in your custom XSL files is to make sure you reference the ddwrt namespace.

xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime
Once this has been added to the rest of your namespace declarations at the top of your <xsl:stylesheet> tag, you should be able to use the “FormatDate” function anywhere you like.  Here is sample of what a full XSL file would look like that does this.

<xsl:stylesheet 
  version="1.0" 
  exclude-result-prefixes="x d xsl msxsl cmswrt"
  xmlns:x="http://www.w3.org/2001/XMLSchema" 
  xmlns:d="http://schemas.microsoft.com/sharepoint/dsp" 
  xmlns:cmswrt="http://schemas.microsoft.com/WebParts/v3/Publishing/runtime"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt"
  xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime">
    
    <xsl:template name="Default" match="*" mode="itemstyle">
 <xsl:value-of select="ddwrt:FormatDate(@ArticleStartDate, 2057, 3)"/>   
    </xsl:template>
   
</xsl:stylesheet>


Here are the details on the different formats you can get by changing the parameters.  March 23 12:00 AM was used as input for all outputs.

OutputLocaleFormat
3/23/200910331
3/23/2009 12:00 AM10332
Monday, March 23 200910333
12:00 AM10334
Monday, March 23, 2009 12:00 AM10337
3/23/2009 12:00:00 AM103313
Monday, March 23, 2009 12:00:00 AM103315
23/03/200920571
3/23/2009 12:00 AM20572
23 March 200920573
00:0020574
23/03/2009 00:0020575
23 March 2009 00:0020577
00:00:00205712
23/03/2009 00:00:00205713
23 March 2009 00:00:00205715


You can also get a list of all the available locale’s here.

Hope this help,
Oumaima Achkari

Monday, May 4, 2015

Custom date formats with XSLT

Custom date formats with XSLT

The default format of a date field is something like 2012-01-01 00:00:00 – this is dependent on the language of the SharePoint installation. It is a very common requirement to display a date field in a different format.
Example
 2012-01-01 00:00:00

Example code
<xsl:value-of select="@ArticleStartDate"/>

Solution

1. Open the ItemStyle.xsl in SharePoint Designer or your favorite editor.
2. To be able to use the FormatDate function, add the DDWRT name space reference in the top section.
xmlns:ddwrt=“http://schemas.microsoft.com/WebParts/v2/DataView/runtime”
3. Use the code below to display the date field in the location you want the date shown. Every language has a different country code. The example in this article uses the UK country code (1033).
<xsl:value-of select="ddwrt:FormatDateTime(string(@ArticleStartDate) ,1033 ,'dd-mm-yyyy')"/>

Result
 01-01-2012
There are many different ways to display dates, take a look on MSDN for all the format specifiers. The country codes (supported locale identifiers) can also be found on MSDN.
Hope this help,
Oumaima

Sharepoint Designer 2013, XSLT List View Options ribbon option is not showing

Sharepoint Designer 2013, XSLT List View Options ribbon option is not showing I have an ordinary Wiki Page, also tried making an Article...