Solved

How can I compare dates using XSL?

Posted on 2016-08-26
8
15 Views
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.
0
Comment
Question by:Martin Spalding
  • 5
  • 3
8 Comments
 
LVL 31

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.
0
 

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?
0
 
LVL 31

Expert Comment

by:Jamie McAllister MVP
ID: 41775258
Any chance you could output some raw dates so we could see the formatting?
0
 

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?

Thanks.
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 31

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" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:date="http://exslt.org/dates-and-times"
extension-element-prefixes="date">

...

<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 -->
</xsl:if>

Open in new window


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

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?

Thanks.
0
 
LVL 31

Accepted Solution

by:
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?
0
 
LVL 31

Expert Comment

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

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
Why do we like using grid based layouts in website design? Let's look at the live examples of websites and compare them to grid based WordPress themes.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

707 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now