Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Chris K 15 posts 36 karma points
    Mar 09, 2012 @ 22:21
    Chris K
    0

    Help with date comparison

    I have some existing XSLT code that I need to change due to a change in the way the page handles/displays "Recent Events".  Tis particular page has been displaying upcoming events in a left column, based off the eventDate field. For multiple day events (ex. week long) it was requested that I implement an added field for content posting call endDate where they could use it as an expiration and the eventDate would remain in use for the display of an event date.

    Example of the existing code:

      <xsl:for-each select="umbraco.library:GetXmlNodeById(1078)/*/*/CustomEventItem [customfunctionlib:includeItem($url, eventItemFilter)] [umbraco.library:DateGreaterThanToday(eventDate)]">
        <xsl:sort select="umbraco.library:FormatDateTime(eventDate, 'yyyyMMddhhmmss')" order="ascending"/>
          <xsl:if test="position() &lt;= $numberOfItems">
            <li>
            <a href="{umbraco.library:NiceUrl(@id)}">
              <span class="smallDate"><xsl:value-of select="umbraco.library:ShortDate(eventDate)"/>
                <xsl:if test="string(locationSummary) != ''">
                  <xsl:text> - </xsl:text><xsl:value-of select="locationSummary"/>
                </xsl:if>
              </span>
            <br/>
            <xsl:value-of select="Title/text()"/>
            </a>
            <br />
            <br />
            </li>
          </xsl:if>
        </xsl:for-each>

    That code works to display a list of events that have not yet passed the event date. Having added a new CustomEventItem called endDate, there are old event postings that do not have dates. changing the first line to read:

    <xsl:for-each select="umbraco.library:GetXmlNodeById(1078)/*/*/CustomEventItem [customfunctionlib:includeItem($url, eventItemFilter)] [umbraco.library:DateGreaterThanToday(endDate)]">

    throws the error: "System.FormatException: String was not recognized as a valid DateTime." so I assume you have to cast for this in some way so that anything null/blank is ignored. Am I missing anything here and can someone help with tweaking this to compare on the newly added field to postings?

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 7x admin c-trib
    Mar 10, 2012 @ 00:39
    Chriztian Steinmeier
    1

    Hi Chris,

    You can add a predicate (filter) before the one that calls the library function, to make sure that only nodes that have a value in the endDate property are sent to the function:

    <xsl:for-each select="
        umbraco.library:GetXmlNodeById(1078)/*/*/
        CustomEventItem[customfunctionlib:includeItem($url, eventItemFilter)]
        [normalize-space(endDate)]
        [umbraco.library:DateGreaterThanToday(endDate)]">

     

    /Chriztian

  • Chris K 15 posts 36 karma points
    Mar 10, 2012 @ 14:19
    Chris K
    0

    Chriztian,

    That's exactly what I needed. I kept trying to write xsl:if statements to handle this, but this is much nicer.

    I appreciate the help and thank you very much!

     

     

Please Sign in or register to post replies

Write your reply to:

Draft