Solved

How can I compare dates using XSL?

Posted on 2016-08-26
8
18 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
FAQ pages provide a simple way for you to supply and for customers to find answers to the most common questions about your company. Here are six reasons why your company website should have a FAQ page
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
This video teaches users how to migrate an existing Wordpress website to a new domain.

786 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