Solved

How can I compare dates using XSL?

Posted on 2016-08-26
8
20 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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 

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

This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
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
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

821 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