How can I compare dates using XSL?

Posted on 2016-08-26
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?
Tutorials alone can't teach real engineering

So we built better training tools.

-Hands-on Labs
-Instructor Mentoring
-Scenario-Based Tests
-Dedicated Cloud Servers

All at your fingertips. What are you waiting for?


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


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 500 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

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!

Question has a verified solution.

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

Because your company can’t afford for you to make SEO mistakes, you’ll want to ensure you’re taking the right steps each and every time you post a new piece of content. This list of optimization do’s and don’ts can help you become an SEO wizard.
A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

691 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