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 thought 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:
&amp;lt;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"&amp;gt;
&amp;lt;xsl:import href="/Style Library/XSL Style Sheets/date_templates.xsl"/&amp;gt;
  • Simply call the templates, passing in the appropriate parameters.  For example, to call the getDateFromDays template:
&amp;lt;xsl:call-template name="getDateFromDays"&amp;gt;
  &amp;lt;xsl:with-param name="paramBaseDate" select="@MyDateColumn"/&amp;gt;
  &amp;lt;xsl:with-param name="paramDelta"&amp;gt;7&amp;lt;/xsl:with-param&amp;gt;
&amp;lt;/xsl:call-template&amp;gt;

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.

Similar Posts

47 Comments

  1. Hi, even though this is already a bit old I guess it’s still useful but I didn’t find any hints on copyright.
    So I’m wondering if it’s legal to simply download the date_templates.xsl and use it in the stylesheets I’m developing for my employer (just at the moment creating excel-files out of some input using xslt and looking for the correct calculation of dates given as a string to put in sheet1.xml formatted as number :-) )?
    Is this some kind of public domain or what would I have to do to avoid problems?

    1. I think you’re fine using the code. Generally speaking, when we publish stuff like this on our blogs, the understanding is that it will be used in some way.

      I tried to ask the author a few years back and was never able to reach him. I say go for it!

      M.

  2. Hello, thank you for the post, this is exactly what I’m looking for, however after adding this to the top of the .aspx page SPD was throwing me an error “A reference to variable or parameter ‘Today’ can not be resolved, The parameter or variable may not be defined, or in scope’ Is there something I’m missing I have entered everything as demonstrated (changing only the @Due_x0200_Date to my field name) any suggestions would be greatly appreciated, I’ve tried tens of different processes to get this very useful and seemingly simple functionality with no success for two weeks now.

    1. DWTK:

      Odds are that Today isn’t defined in the ParameterBindings section or declared as a parameter in the XSL.

      In the ParameterBindings:

      <ParameterBinding Name="Today" Location="CAMLVariable" DefaultValue="CurrentDate"/>

      And at the top of the XSL section, below xsl:output

      <xsl:param name="Today"/>

      Once you have those two bits in place, the $Today variable ought to be available to you.

      M.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.