3 Replies Latest reply on Aug 22, 2012 1:00 PM by vladodias

    How to find number of days between 2 date elements in XSLT

    Anju
      Hi,

      I need to calculate number of days between 2 date elements(xs:date type). Can some one please direct me as to how I can do the same.

      I am working in 11g and using XSLT 1.0. I tried various options but still not got a solution for it. I think it can be done using XSLT 2.0 but that also didnt work for me.

      Can someone please help him solve this, Thanks in advance!!

      Thanks,
      Anju
        • 1. Re: How to find number of days between 2 date elements in XSLT
          vladodias
          Hi,

          Have you seen this post?
          Re: Get Date Difference between 2 date values as days

          You can do it in XSLT since the dates are in ISO 8601 format...
          http://www.w3.org/TR/NOTE-datetime

          Here is a sample XSLT...
          <xsl:stylesheet  version="2.0"
          xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
          xmlns:xs="http://www.w3.org/2001/XMLSchema"
          xmlns:fn="http://www.w3.org/2005/xpath-functions">
          <xsl:template match="/">
          <xsl:variable name="date1" select="xs:dateTime(/root/date1)"/>
          <xsl:variable name="date2" select="xs:dateTime(/root/date2)"/>
          <xsl:value-of select="fn:days-from-duration($date2 - $date1)"/>
          </xsl:template>
          </xsl:stylesheet>
          The above XSLT will result *4* for the following input...
          <root>
          <date1>2012-01-11T00:00:00.000-05:00</date1>
          <date2>2012-01-15T00:00:00.000-05:00</date2>
          </root>
          You can test this example here...
          http://xslttest.appspot.com/

          Hope this helps...

          Cheers,
          Vlad

          It is considered good etiquette to reward answerers with points (as "helpful" - 5 pts - or "correct" - 10pts)
          https://forums.oracle.com/forums/ann.jspa?annID=893
          • 2. Re: How to find number of days between 2 date elements in XSLT
            Anju
            Thanks Vlad!!

            Just saw the same post some time back before getting your reply.. Was about to reply on this thread :):)

            Just one question though

            We are using XSLT 1.0 but to use these functions I changed the version from 1.0 to 2.0 in the XSLT , is this the right way to do it or is there some other way to change version of XSLT and use it.

            Thannks,
            Anju
            • 3. Re: How to find number of days between 2 date elements in XSLT
              vladodias
              I changed the version from 1.0 to 2.0 in the XSLT , is this the right way to do it?
              Yes, that's the way. At least there's no other way that I'm aware of... You may loose the design view though, as JDEV design view only really supports 1.0...
              1 person found this helpful