Monday, October 22, 2007

SharePoint: Comparing dates in XSLT

In one of my sharePoint projects, I had to compare a date with the current date in XSLT. As it turned out, there is no support for date comparision in XSLT.
This is how I went about doing it.

The trick involves converting the date string into a number
Say the date is in ISO format YYYY-MM-DDTHH:MM:SSZ (for example, 2001-01-21T22:54:47Z).

Say we have a variable DueDate
<xsl:variable name="DueDate" select="'2001-01-21T22:54:47Z'">

Replace the dashes in the string with empty string
Take the first 10 character in the date sring
convert the string to number
number(translate(substring(@DueDate,1,10),'-',''))

On doing the above we get: 20010121


We can do the same thing with the date to compare. We can compare the duedate with the current date in SharePoint as follows
number(translate(substring(@DueDate,1,10),'-','')) & lt;number(translate(substring(ddwrt:TodayIso(),1,10),'-',''))

You can apply the same logic to compare dates in different formats.

Digg It! Add to del.icio.us Stumble This