Bug With SharePoint 2010 XSLT DateFormat Function

 

The other day I found out that there is a problem/bug using the XSLT ddwrt:DateFormat function. The problem/bug occurs when the regional settings is not set to English (United States).

When a day has a value between 1 and 12, the ddwrt:DateFormat function will recognize this as the month. The month value will be used as the day.

So if the day value is less than 13, you get the following problem:


US Date Format ddwrt:DateFormatTime(date, 1043, ‘dd-MMMM-yyyy’)
8/6/2011 8-June-2011
2/3/2011 2-March-2011

When the day value is larger than 13, you get the correct formatting:


US Date Format ddwrt:DateFormatTime(date, 1043, ‘dd-MMMM-yyyy’)
8/19/2011 19-August-2011
2/28/2011 28-February-2011

I did not notice it in the beginning, because my settings are always set to English (United States) on my development server. When I implemented the XSLT stylesheet on a different environment, where the regional settings were set to Dutch (Belgium), some of dates were incorrectly formatted.

English (United States)

2011-07-29-BugSP2010XSLT-01.png
English (United States) Regional Setting

2011-07-29-BugSP2010XSLT-02.png
English date format converted to Dutch date format

Dutch (Belgium)

2011-07-29-BugSP2010XSLT-03.png
Dutch (Belgium) Regional Setting

2011-07-29-BugSP2010XSLT-04.png
Conversion problem with the Dutch date format

Solution

After some research and testing without result, I did some googling and found a post from someone who had the same problem. The blog post can be found here. In this blog post he included a custom XSL template to format the date.

I have created my own version of his XSL template to format the date in Dutch date format.

XSL Template

<xsl:template name="FormatDutchDate">
        <!– Template Parameters –>
        <xsl:param name="dateValue" />
        <xsl:param name="monthFormat" /> 
         
        <!– Split Date –>
        <xsl:variable name="day" select="substring-before($dateValue, ‘/’)" />
        <xsl:variable name="month" select="substring(substring-after($dateValue, ‘/’), 1, 2)" />
        <xsl:variable name="year" select="substring(substring-after(substring-after($dateValue, ‘/’), ‘/’), 1, 4)" />
         
        <!– Create US Date Format –>
        <xsl:variable name="USDate">
                <xsl:value-of select="$month" />/<xsl:value-of select="$day" />/<xsl:value-of select="$year" />
        </xsl:variable>
         
        <!– Month Notation –>
        <xsl:variable name="monthString">                                                
                <xsl:choose>
                        <xsl:when test="$monthFormat=’MM’">
                                <xsl:value-of select="$month" />
                        </xsl:when>
                        <xsl:when test="$monthFormat=’MMM’">
                                <xsl:value-of select="ddwrt:FormatDateTime($USDate, 1043, ‘MMM’)" />
                        </xsl:when>
                        <xsl:otherwise>
                                <xsl:value-of select="ddwrt:FormatDateTime($USDate, 1043, ‘MMMM’)" />
                        </xsl:otherwise>
                </xsl:choose>
        </xsl:variable>
         
        <!– Create Date –>
        <xsl:choose>
        <xsl:when test="string-length($day) = 1">0</xsl:when>
        </xsl:choose>
        <xsl:value-of select="$day" />-<xsl:value-of select="$monthString" />-<xsl:value-of select="$year" />
</xsl:template>

Call the XSL template

<xsl:call-template name="FormatDutchDate">
        <xsl:with-param name="dateValue" select="@Expires" />
        <xsl:with-param name="monthFormat">MMM</xsl:with-param>
</xsl:call-template>

Result

2011-07-29-BugSP2010XSLT-05.png

XSLT Month Options

<xsl:with-param name="monthFormat">MM</xsl:with-param>

2011-07-29-BugSP2010XSLT-06.png
MM – Number notation

<xsl:with-param name="monthFormat">MMM</xsl:with-param>

2011-07-29-BugSP2010XSLT-07.png
MMM – Short Name Format

<xsl:with-param name="monthFormat">MMMM</xsl:with-param>

 

 2011-07-29-BugSP2010XSLT-08.png
MMMM – Full Name Format