How can I compare dates using XSL?

Posted on 2016-08-26
Medium Priority
Last Modified: 2016-09-18
I have the below condition set within my xsl code:

<xsl:if test="ddwrt:FormatDateTime(string(@ExpiryDate), 1033, 'yyyyMMdd') &gt;= ddwrt:FormatDateTime(string(ddwrt:Today()), 1033, 'yyyyMMdd')">DO SOMETHING</xsl:if>

Open in new window

The @ExpiryDate is a SharePoint column, this is why I am formatting the dates (expiry date and today date) to the same structure and language before comparison.

Strangely, when todays date is 26/08/2016 and the expiry date is 01/09/2016 the statement is not executed, however if todays date is 26/08/2016 and the expiry date is 09/09/2016 the statement is executed? I'm presuming the format isn't quite right somehow but I am finding it hard to test/debug.

Any help appreciated.
Question by:Martin Spalding
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
LVL 32

Expert Comment

by:Jamie McAllister MVP
ID: 41771868
I suspect your source data is returning the dates in US format as MM/DD and the format isn't picking that up.

That's why 9/9 works.

Author Comment

by:Martin Spalding
ID: 41774681
So my FormatDateTime to 'yyyyMMdd' isn't happening? Can you suggest any fixes? Any code snippets to help?
LVL 32

Expert Comment

by:Jamie McAllister MVP
ID: 41775258
Any chance you could output some raw dates so we could see the formatting?
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

by:Martin Spalding
ID: 41775884
That's the thing, the date is passed in as a datafield (@ExpiryDate), Using javascript I would output this value via "alert(@ExpiryDate)" but using xsl I don't know how this can be outputted?

LVL 32

Expert Comment

by:Jamie McAllister MVP
ID: 41776092
@ExpiryDate should output it I believe.

Another comparison approach would be the below. This is taking out the delimiters (dashes in this case) and doing a comparison of the raw numbers rather than as a date, assuming YearMonthDay format. ;

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" 


<xsl:for-each select="forms/form">

<xsl:variable name="today" select="translate(substring-before(date:date-time(), 'T'), '-', '')"/>
<xsl:variable name="start" select="translate(start, '-', '')"/>
<xsl:variable name="end" select="translate(end, '-', '')"/>

<xsl:if test="$start &lt;= $today and $today &lt;= $end">
    <!-- in progress -->

Open in new window

See http://stackoverflow.com/questions/20342805/xslt-1-0-compare-dates

Author Comment

by:Martin Spalding
ID: 41776246
The above code returns nothing so is a backward step for me. When you say @ExpiryDate should output the value what do you mean? How would I display this in an alert box for example?

LVL 32

Accepted Solution

Jamie McAllister MVP earned 2000 total points (awarded by participants)
ID: 41776618
Hi Martin,

I just meant that a standard select would output the value as text from your transform, so;

<xsl:value-of select="@ExpiryDate"/>

However I guess you could wrap an alert around that if you wanted;

alert(''<xsl:value-of select="@ExpiryDate"/>'');

To say that the XSL in the sample returns nothing, you mean the If didn't match anything? You replaced <!-- in progress --> with an action?
LVL 32

Expert Comment

by:Jamie McAllister MVP
ID: 41803568
Working sample provided.

Featured Post

Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In case you ever have to remove a faulty web part from a page , add the following to the end of the page url ?contents=1
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question