Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding the difference between 2 dates in xslt

Is there a less then kludgey way of finding the difference in days between 2 dates in xslt? If so can you point me in the right direction. I am receiving dates in the format of mm/dd/yyyy.

like image 227
Mike T Avatar asked Apr 04 '11 21:04

Mike T


1 Answers

A nicer (and shorter) alternative for XSLT 1.0 is to compute the equivalent Julian dates and subtract them.

Template:

<xsl:template name="calculate-julian-day">
    <xsl:param name="year"/>
    <xsl:param name="month"/>
    <xsl:param name="day"/>

    <xsl:variable name="a" select="floor((14 - $month) div 12)"/>
    <xsl:variable name="y" select="$year + 4800 - $a"/>
    <xsl:variable name="m" select="$month + 12 * $a - 3"/>

    <xsl:value-of select="$day + floor((153 * $m + 2) div 5) + $y * 365 + floor($y div 4) - floor($y div 100) + floor($y div 400) - 32045"/>

Usage:

<xsl:variable name="dateInv" select="'20120406'" />
<xsl:call-template name="calculate-julian-day">
    <xsl:with-param name="year" select="substring($date,1,4)"/>
    <xsl:with-param name="month" select="substring($date,5,2)"/>
    <xsl:with-param name="day" select="substring($date,7,2)"/>
</xsl:call-template>

Repeat for the second date and you'll have two integers. Then, simply subtract them.

like image 173
Buffalo Avatar answered Sep 19 '22 19:09

Buffalo