Tuesday, June 26, 2007

Comparing Dates in SharePoint XSL

Here's a way to peform a date comparison in the XSLT definition of a data view web part:

<xsl:if test="number(translate(substring-before(ddwrt:FormatDate(@Date_x0020_Raised ,1053 ,5), ' '), '-', ''))+2 < number(translate(substring-before(ddwrt:TodayIso(), 'T'), '-', ''))">
        
<img src="_layouts/images/ewr210s.gif" />
</
xsl:if>


This compares the date in the "Date Raised" field with the current date, and if the "Date Raised" is more than 2 days ago then an image is displayed.

6 comments:

Knrs said...

Thank you so much! :-)

Victheslick said...

Thankyou! This was hard to find online

Ben L said...

I don't think the comparison is correct between month end and month start.

i.e. Between Jan31 and Feb01
((20080131 + 2) < 20080201) is true when there's only 1 day in between.

testblog said...

My bacon has been saved TodayIso(), sir, and you are the one that saved it. Many thanks.

Rich Rockwell said...

Thanks for this post. I needed to compare the current date to the date a year ago. I was able to adapt your code to do this as follows.

This sample code compares a field called "Last Updated" plus one year to today's date. If the last update was more than a year ago, make it red. It only compares dates if the field is not blank.


<xsl:if test="normalize-space(@Last_x0020_Updated) != '' and concat(substring(@Last_x0020_Updated,1,4)+1,substring(@Last_x0020_Updated,6,2),substring(@Last_x0020_Updated,9,2)) < concat(substring-before(ddwrt:TodayIso(),'-'),substring(ddwrt:TodayIso(),6,2),substring(ddwrt:TodayIso(),9,2))">background-color: #FF0000;</xsl:if>

It formats the date so if the last update was October 22, 2007 and today is November 24, 2008, it will compare 20081022 to 20081123.

What I would really find helpful is a julian date format option so you could compare dates as numbers.

Tim said...

Thanks. Here is one that helped me get the expired values for a data view: