Date Arithmetic in SharePoint DVWPs

I posted last September about Date Calculations in a DVWP.  In that post, I pointed out Andy Lewis’ great post about how to do Filtering and Formatting with Date Values over at the Microsoft SharePoint Designer Team Blog.

I’ve been answering quite a few questions about date arithmetic over at the MSDN forums lately, so I though an updated post was in order to explain a little bit more about how you can use the templates that Andy provides.  Here are the steps I would suggest:

  • Download the date_templates.xsl file.
  • Put date_templates.xsl into your Site Collection.  I usually put my reusable XSL into /Style Library/XSL Style Sheets in MOSS, or I create a Document Library in the root site in WSS.
  • Include the templates in your DVWP.  Assuming that you’ve used the location above, add the <xsl:import> line into your DVWP immediately below the <stylesheet line, like so:
<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="/Style Library/XSL Style Sheets/date_templates.xsl"/>
  • Simply call the templates, passing in the appropriate parameters.  For example, to call the getDateFromDays template:
<xsl:call-template name="getDateFromDays">
  <xsl:with-param name="paramBaseDate" select="@MyDateColumn"/>
  <xsl:with-param name="paramDelta">7</xsl:with-param>
</xsl:call-template>

To understand what you need to pass into each template, you’ll need to dive into the date_templates.xsl file a little, but Andy’s documented the XSL well.  Each template will require a set of parameters like getDateFromDays above.