How to extract day, month and year from xslt in ItemStyle.xsl?
-
10-12-2019 - |
Question
I have created custom template for CQWP in ItemStyle.
My code for template is:
<xsl:template name="CustomLofbergs" match="Row[@Style='CustomLofbergs']" mode="itemstyle">
<div class="main">
<div class="subDateText">
<div class="subDate">
<div class="day">
13 //I want to display the date here
</div>
<div class="monthyear">
<div class="month">
05 // Display month here
</div>
<div class="year">
1990 // Display year here.
</div>
</div>
</div>
<div class="subText">
<div class="eventTile">
<xsl:variable name="SafeLinkUrl">
<xsl:call-template name="OuterTemplate.GetSafeLink">
<xsl:with-param name="UrlColumnName" select="'LinkUrl'"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="DisplayTitle">
<xsl:call-template name="OuterTemplate.GetTitle">
<xsl:with-param name="Title" select="@Title"/>
<xsl:with-param name="UrlColumnName" select="'LinkUrl'"/>
</xsl:call-template>
</xsl:variable>
<div class="item link-item">
<xsl:call-template name="OuterTemplate.CallPresenceStatusIconTemplate"/>
<a href="{$SafeLinkUrl}" title="{@LinkToolTip}">
<xsl:if test="$ItemsHaveStreams = 'True'">
<xsl:attribute name="onclick">
<xsl:value-of select="@OnClickForWebRendering"/>
</xsl:attribute>
</xsl:if>
<xsl:if test="$ItemsHaveStreams != 'True' and @OpenInNewWindow = 'True'">
<xsl:attribute name="onclick">
<xsl:value-of disable-output-escaping="yes" select="$OnClickTargetAttribute"/>
</xsl:attribute>
</xsl:if>
<xsl:value-of select="$DisplayTitle"/>
</a>
</div>
</div>
<div class="eventDescription">
<xsl:value-of select="@Description"/>
</div>
</div>
</div>
</div>
</xsl:template>
The template is for displaying events. So, I knew I have to use EventDate field but not having actual idea how to do..??
I used <xsl:value-of select="ddwrt:FormatDateTime(@EventDate,1033,'d')"/>
but not working for me.
What should I do?
Solution
Try this:
<xsl:variable name="date" select="ddwrt:FormatDate(string(@EventDate), 1033, 1)"/>
<xsl:variable name="day" select="format-number(substring-before(substring-after($date,'/'),'/'),'00.')"/>
<xsl:variable name="month" select="format-number(substring-before($date,'/'),'00.')"/>
<xsl:variable name="year" select="format-number(substring(substring-after(substring-after($date,'/'),'/'),1,4),'00.')"/>
<!-- now you can use these variables in your code: -->
<div class="subDate">
<div class="day">
<xsl:value-of select="$day"/>
</div>
<div class="monthyear">
<div class="month">
<xsl:value-of select="$month"/>
</div>
<div class="year">
<xsl:value-of select="$year"/>
</div>
</div>
</div>
If you work with dates often in XSLT, I would also recommend you to have a look at this date library for SharePoint:
(this is from Andy Lewis's post Filtering and formatting with Date Values)
The library includes great amount of various useful templates: calculating amount of days between dates, formatting dates, etc. I used it myself many times and find the library quite reliable and comprehensive.
P.S. Btw, in SharePoint 2013, I would try to avoid using XSLT. There is CSWP and there is CSR... I'm not saying that you must drop your work and go learn these new things - no, but at least start exploring them would be a good idea ;)...