title: XSL Date & Time Difference Calculation (XSLT 1.0)
body: <p>Using XSLT 1.0 &amp; receive dates in the following format:</p>
  
  <pre><code>20111129060804
  </code></pre>
  
  <p>and I have a need to be able to calculate differences between two of them.  However, <strong>I cannot use any extensions</strong>. </p>
  
  <p>I also have other limitations as well:</p>
  
  <ul>
  <li>I am limited as to the namespaces that may be used inside an XSLT,</li>
  <li>and where I am using the xsl stylesheet (a 3rd party application) it
  will not properly process any include/import actions within the xsl.</li>
  </ul>
  
  <p>So in effect, the xslt that I need to use must be pretty much self-inclusive, in that whatever is called or referenced must be in the xslt itself.  </p>
  
  <p>I had given thought to taking the date &amp; time &amp; converting it to a Julian serial number &amp; handling the date math much the way that Excel does.  Example - </p>
  
  <pre><code>09/14/2011 08:19:37 = 40800.346956018500000
  09/15/2011 12:22:46 = 40801.515810185200000
  difference 1 day 4 hours 3 minutes and 9 seconds
  difference = 1.168854166666280
  </code></pre>
  
  <p>where everything left of the decimal is in Days, and everything right of the decimal is in time (i.e. .168854166666280 * 24 = 4.0524999999990680, or 4 hours and .0525 percent of an hour, or 3.15 minutes).</p>
  
  <p>Has anyone ever done this type of elapsed date-time math in XSLT 1.0 without the use of extensions?  </p>
  
  <p>Would appreciate any thoughts, suggestions or references if there's an existing set of code I can re-purpose.</p>
  
  <p>EDIT - </p>
  
  <p>I decided to proceed with the above, and have hit a snag on conversion of TIME.</p>
  
  <p>The example I am using is:
  Start Time = 08:19:37
  End Time = 15:58:33</p>
  
  <p>which is a duration/difference of 7:38:56 (BTW, this is only for same-day calculation - I have it to throw an error if Endtime &lt; Start Time or if the Day is > 0). </p>
  
  <p>To arrive at the time, I convert everything to seconds &amp; then do the math.
  08:19:37 converts to 29977 (8 * 3600 for hours + 19 * 60 for minutes + seconds).
  15:58:33 converts to 57513 (15 * 3600 for hours + 58 *60 for minutes + seconds).</p>
  
  <p>The difference is 27536 seconds.</p>
  
  <p>I get:
  27536/3600 =7.6488888888888888888 &lt;-- the integer part is hours
  then the difference of 27536 - 25200 = 2336 &lt;-- the 25200 = 7 hours * 3600 seconds
  2336/60 = 38.93333333333333333 &lt;-- the integer part is 38 minutes
  then the difference of 2336 - 2280 = 56 seconds from above. </p>
  
  <p>However, if I try to grab the integer portion of these numbers, its subject to rounding.</p>
  
  <pre><code>&lt;xsl:template name="time-difference"&gt;  
  &lt;xsl:param name="from-hour"/&gt;  
  &lt;xsl:param name="from-minute"/&gt;  
  &lt;xsl:param name="from-second"/&gt;  
  &lt;xsl:param name="to-hour"/&gt;  
  &lt;xsl:param name="to-minute"/&gt;  
  &lt;xsl:param name="to-second"/&gt;  
  &lt;xsl:variable name="f-secs" select="($from-hour * 3600) + ($from-minute * 60) + ($from-second)"/&gt;  
  &lt;xsl:variable name="t-secs" select="($to-hour * 3600) + ($to-minute * 60) + ($to-   second)"/&gt;  
  &lt;xsl:variable name="sec-diff" select="$t-secs - $f-secs"/&gt;  
  &lt;xsl:variable name="daysec-diff" select="format-number(($sec-diff div 86400),'#')"/&gt;  
  &lt;xsl:variable name="q" select="($sec-diff div 3600)"/&gt;  
  &lt;xsl:variable name="t-hrs-diff" select="format-number(($sec-diff div 3600),'00')"/&gt;  
  </code></pre>
  
  <p> </p>
  
  <p>The variable q above has 7.648888888888888888 (which is correct)
  however, the t-hrs-diff once the format-number happens now has "08" in it, making my time incorrect.</p>
  
  <p>I know any other formatting attempt to grab the integer portion of the number would result in rounding.</p>
  
  <p>Outside of the <strong>substring-before</strong> &amp; <strong>substring-after</strong> - which would (<strong>I think</strong>) make me have to convert the result to a number before using in the next calculation - is there ANY other way to grab the unaltered integer portion of the number &amp; not have to do conversion from a string to number to use the pieces?</p>