Date format is not correct in the DataFormWebPart SharePoint 2010

Ok there is some problem when formatting date’s which is different than US format when using XsltListViewWebPart has defined by Microsoft. This happens because the ddwrt:FormatDate function expects the data format to be in UTC and the XSLTListViewWebPart returns the date preformatted to the Web Locale or Locale Date/Time .

To fixing this issue Microsoft has released a new feature ,A new Property, EnableOriginalValue, has been added to the XlstListViewWebPart that unblocks this scenario by February CU 2012.

You can check fallowing article for more information:
http://support.microsoft.com/kb/2580994

For DataFormWebPart;the option is to use the DataFormWebPart with the following property set to false on the <SPDataSource> tag: UseServerDataFormat.
Make UseServerDataFormat property set to false the DataFormWebPart will receive the Date/Time in UTC format and then the ddwrt:FormatDate function will work accurately.

Or you can use power of xslt to format date as you wish:
Fallowing example will help you to how to do it.

Add fallowing codes in DataFormWebPart for suitable place for xsl:template

<xsl:template name=”FormatCorrectDate”>
<xsl:param name=”dateValue” />
<xsl:param name=”monthFormat” />

<!– Sperater is important here it should be “.” or “/” according your current language settings –>
<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=’M1′”>
<xsl:value-of select=”$month” />
</xsl:when>

<xsl:when test=”$monthFormat=’M2′”>
<xsl:value-of select=”ddwrt:FormatDateTime(string($USDate), 1055, ‘MMM’)” />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select=”ddwrt:FormatDateTime(string($USDate), 1055, ‘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 formatting template where you can needed in code :

<xsl:call-template name=”FormatCorrectDate”>
<xsl:with-param name=”dateValue” select=”string(@Created)” />
<xsl:with-param name=”monthFormat”>M2</xsl:with-param>
</xsl:call-template>

Resources:
https://www.nothingbutsharepoint.com/sites/eusp/Pages/Bug-With-SharePoint-2010-XSLT-DateFormat-Function.aspx
http://social.msdn.microsoft.com/forums/en-US/sharepointcustomization/thread/7a636804-1ff1-4bb8-8bcd-9ef713d15680
http://support.microsoft.com/kb/2580994

Advertisement