RE: [xsl] xslt 2. Date arithmetic.

Subject: RE: [xsl] xslt 2. Date arithmetic.
From: "Pawson, David" <David.Pawson@xxxxxxxxxxx>
Date: Wed, 2 Mar 2005 10:15:35 -0000
Messy, but works.
Thanks David, Michael.

regards DaveP


<!-- Subtract two dates -->
<!-- result is t1 - t2 -->

<xsl:function name="dp:subDates" >
   <xsl:param name="t1" as="xs:date"/>
   <xsl:param name="t2" as="xs:date"/>

   <xsl:variable name="t1Months" select="month-from-date($t1)"/>
   <xsl:variable name="t1Years" select="year-from-date($t1)"/>
   <xsl:variable name="t2Months" select="month-from-date($t2)"/>
   <xsl:variable name="t2Years" select="year-from-date($t2)"/>

   <xsl:choose>
   <xsl:when test="($t1Years - $t2Years) &lt; 0">
     <xsl:value-of select="'Negative Time'"/>
   </xsl:when>
   <xsl:when test="($t1Years - $t2Years) = 0">
     <xsl:choose>
       <xsl:when test="($t1Months &lt; $t2Months)">
         <xsl:value-of select="'Negative Time'"/>
       </xsl:when>
       <xsl:otherwise>
         <xsl:sequence select="xs:string($t1Months - $t2Months)" /><xsl:text>
months</xsl:text>
       </xsl:otherwise>
     </xsl:choose>
   </xsl:when>
   <xsl:otherwise>

     <xsl:variable name="years" select="xs:string(
       if ($t1Months &lt; $t2Months)
        then
         $t1Years - $t2Years - 1
        else
         $t1Years - $t2Years)" as="xs:string" />

     <xsl:variable name="months" select="xs:string(
       if ($t1Months &lt; $t2Months)
       then
        $t1Months + 12 - $t2Months
       else
        $t1Months - $t2Months)" as="xs:string"/>
     <xsl:message>
yrs       <xsl:value-of select="$years"/>
mnths      <xsl:value-of select="$months"/>
t1m      <xsl:value-of select="$t1Months"/>
t1y      <xsl:value-of select="$t1Years"/>
t2m      <xsl:value-of select="$t2Months"/>
t2y      <xsl:value-of select="$t2Years"/>

   </xsl:message>


     <xsl:sequence select="string-join(($years,'years',$months,'months'),'
')"/>


   </xsl:otherwise>
 </xsl:choose>
</xsl:function>

--
DISCLAIMER:

NOTICE: The information contained in this email and any attachments is
confidential and may be privileged.  If you are not the intended
recipient you should not use, disclose, distribute or copy any of the
content of it or of any attachment; you are requested to notify the
sender immediately of your receipt of the email and then to delete it
and any attachments from your system.

RNIB endeavours to ensure that emails and any attachments generated by
its staff are free from viruses or other contaminants.  However, it
cannot accept any responsibility for any  such which are transmitted.
We therefore recommend you scan all attachments.

Please note that the statements and views expressed in this email and
any attachments are those of the author and do not necessarily represent
those of RNIB.

RNIB Registered Charity Number: 226227

Website: http://www.rnib.org.uk

Current Thread