XSLT and XPath 1.0 Date Arithmetic Templates

SharePoint 2007 and 2010 only support XPath 1.0 which lacks date arithmetic functions.1 To manipulate dates in your DVWP or XLV in SharePoint Designer, you can create your own XSL templates. Great templates are already available like those found in Andy Lewis’ post Filtering and Formatting with Date Values. However, none of the templates I found were a perfect fit for my requirements. So I created my own.

These templates use seconds since the Epoch and are leap year accurate. They are similar to VB.NET’s DateDiff and DateAdd functions and give the same or more precise results. You can try a working example and download the source files from my development examples site.

To use in SharePoint 2007 and 2010, import the date_arithmetic.xsl style sheet.

<XSL><xsl:stylesheet xmlns:x="http://www.w3.org/2001/XMLSchema" xmlns:d="http://schemas.microsoft.com/sharepoint/dsp" version="1.0" exclude-result-prefixes="xsl msxsl ddwrt" xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime" 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">
<xsl:import href="Styles/date_arithmetic.xsl" />
<xsl:output method="html" indent="no"/>

Then call the templates using a date formatted as yyyymmdd. This example is for SP 2007 which uses ISO date format (YYYY-MM-DDTHH:MM:SSZ) by default.

<xsl:template name="dvt_1.rowview">
<tr>
...
<td class="ms-vb">
<xsl:call-template name="DateAdd" >
<xsl:with-param name="yyyymmddDate" select="translate(substring-before(@Created,'T'),'-','')" />
<xsl:with-param name="dateInterval" select="'d'" />
<xsl:with-param name="numberToAdd" select="-120" />
</xsl:call-template>
</td>

</tr>
</xsl:template>

Resulting in:
Transformation Results

And this is an example for an XLV in SP 2010:

...
<xsl:variable name="created">
<xsl:apply-templates select="." mode="PrintFieldWithECB">
<xsl:with-param name="thisNode" select="$thisNode" />
</xsl:apply-templates>
</xsl:variable>
<xsl:variable name="thedate">
<xsl:call-template name="DateAdd">
<xsl:with-param name="yyyymmddDate" select="ddwrt:FormatDateTime(substring-before($created,' '),1033,'yyyyMMdd')" />
<xsl:with-param name="dateInterval" select="'d'" />
<xsl:with-param name="numberToAdd" select="-121" />
</xsl:call-template>
</xsl:variable>
<xsl:value-of select="$thedate" />
...

Resulting in:
2010Transformation Results
The top webpart is a DVWP where -120 is added to the create date. The lower is an XLV with -121 added to the create date.

The templates are not comprehensive and, as of yet, do not support time. But I hope they will give you a head start on your way to creating your own date templates for SP 2007/2010.


1. Recently, Marc D. Anderson posted an article about an undocumented ddwrt function – DateTimeTick – that you can use to find the difference between dates: Calculate Days between Two SharePoint List Dates in XSL Using ddwrt:DateTimeTick

Tagged with: ,
Posted in SharePoint, XML XSL XSLT XPath
3 comments on “XSLT and XPath 1.0 Date Arithmetic Templates
  1. Dedra says:

    Thanks to Oliver Becker (http://obqo.de/) for noticing I hadn’t front-zero padded all the day variables in the EpochtoDate function. Per his suggestion, it’s fixed now!

  2. Rajinikanth says:

    Thanks alot for the work !
    I just want to let you know reg a catch in adddate template. Whats happening is that if I give ‘2013-01-01′ as start date and calculate for ‘-7′ days, it is giving me ’01/-6/2013′.

    Thanks,
    Rajini.

Advertisement