1

I'm using XSLT 2.0, and am trying to calculate the in WS-Trust messages, where there're and elements, so I need to determine the number of days, fractionally, between two date/timestamps, e.g., between:

2014-06-28T03:00:12Z and 2014-06-26T13:00:02Z

I've tried using:

<xsl:sequence select="fn:days-from-duration(xs:dateTime('2014-06-26T13:00:02Z')-xs:dateTime('2014-06-27T13:00:02Z'))"/>

but the above just keeps giving me "-1", i.e., an integer.

If I could maybe get the hours or minutes duration, I might possibly calculate the fractional days, but I tried using:

<xsl:sequence select="fn:hours-from-duration(xs:dateTime('2014-06-26T13:00:02Z')-xs:dateTime('2014-06-27T13:00:02Z'))"/>

or:

<xsl:sequence select="fn:minutes-from-duration(xs:dateTime('2014-06-26T13:00:02Z')-xs:dateTime('2014-06-27T13:00:02Z'))"/>

and those both just give me "0".

Can anyone tell me how I can calculate the duration in either hours or minutes?

Thanks!

user555303
  • 1,146
  • 3
  • 20
  • 44

2 Answers2

1

days-from-duration(), hours-from-duration() and minutes-from-duration() are extraction functions: each extracts a single component from a duration. In your example, you are getting -1, 0 and 0 respectively because the duration between your two dateTimes is -1 days, 0 hours and 0 minutes.

If you want to calculate duration as fractional days, try perhaps:

<xsl:variable name="duration" select="xs:dateTime('2014-06-27T12:36:00Z')-xs:dateTime('2014-06-26T00:00:00Z')" />

<xsl:value-of select="days-from-duration($duration) + hours-from-duration($duration) div 24 + minutes-from-duration($duration) div 1440"/>

This returns 1.525, representing a duration of P1DT12H36M (1 day, 12 hours and 36 minutes).

michael.hor257k
  • 113,275
  • 6
  • 33
  • 51
1

Subtract the two dateTime values to get a dayTimeDuration, then divide this by xs:dayTimeDuration('P1D') (using the div operator) to get the number of days as an xs:double.

michael.hor257k
  • 113,275
  • 6
  • 33
  • 51
Michael Kay
  • 156,231
  • 11
  • 92
  • 164