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.

Date Calculations in a DVWP

Andy Lewis over at the Microsoft SharePoint Designer Team Blog has a nice post about how to do Filtering and Formatting with Date Values.  The best part of the post (it is FULL of useful information) is a set of utility templates that do some useful calculations on dates:

getDateFromDays Outputs the date that is $Delta days from $BaseDate
convertCalcDateValue Formats a calculated date field value in ISO format
getDayDelta Returns the difference in days between paramDateA and paramDateB
getDateRange From a base date it is given, returns START OR END date for MONTH OR QUARTER (LAST, THIS, or NEXT)
isLeapYear Is this paramYear a leap year?
getQuarterMonthBoundary Return START or END date for the quarter inside which paramMonth exists.
countDaysInDate How many days are there between 0 and paramDate (NOT INCLUDING leap-year days)?
countDaysInDateWithLeapYearDays How many days are there between 0 and paramDate (INCLUDING leap-year days)?
ConvertMonthToDays How many days are in paramMonth (including leap-year day if paramYear is supplied)?
ConvertMonthToTotalDays How many days are there from beginning of year through paramMonth (including leap-year day if paramYear is supplied)?
ConvertDaysToMonth What is the month when paramDays have passed since beginning of year?

This set of utility functions can be built upon for all sorts of useful date manipulation, which can be difficult in XSL.