# Time Difference, Conversion & Formatting In Xslt 1.0

## Recommended Posts

I am working in xslt 1.0, and cannot use extensions & am limited as to what namespaces can be used (currently only http://www.w3.org/1999/XSL/Transform can be used). Another limitation I am working with is the 3rd party application I am using will not properly process includes or imports into the XSL. In other words - 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 & nowhere else. I had decided to try to do a conversion to Julian date & mimic how Excel might do the conversion. I've got the date part figured out, but the hour part is what I'm having trouble with. The example I am using is:Start Time = 08:19:37End Time = 15:58:33 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 < Start Time or if the Day is > 0). To arrive at the time, I convert everything to seconds & 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). The difference is 27536 seconds. I get:27536/3600 =7.6488888888888888888 <-- the integer part is hoursthen the difference of 27536 - 25200 = 2336 <-- the 25200 = 7 hours * 3600 seconds2336/60 = 38.93333333333333333 <-- the integer part is 38 minutesthen the difference of 2336 - 2280 = 56 seconds from above. However, if I try to grab the integer portion of these numbers, its subject to rounding. <xsl:template name="time-difference"> <xsl:param name="from-hour"/> <xsl:param name="from-minute"/> <xsl:param name="from-second"/> <xsl:param name="to-hour"/> <xsl:param name="to-minute"/> <xsl:param name="to-second"/><xsl:variable name="f-secs" select="(\$from-hour * 3600) + (\$from-minute * 60) + (\$from-second)"/><xsl:variable name="t-secs" select="(\$to-hour * 3600) + (\$to-minute * 60) + (\$to-second)"/><xsl:variable name="sec-diff" select="\$t-secs - \$f-secs"/><xsl:variable name="daysec-diff" select="format-number((\$sec-diff div 86400),'#')"/><xsl:variable name="q" select="(\$sec-diff div 3600)"/><xsl:variable name="t-hrs-diff" select="format-number((\$sec-diff div 3600),'00')"/>.......</xsl:template> 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. I know any other formatting attempt to grab the integer portion of the number would result in rounding. Outside of the substring-before & substring-after - which would (I think) 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 & not have to do conversion from a string to number to use the pieces? Is there a better way to do this, given the limitations I have? I would appreciate any thoughts or suggestions. Thanks!

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.