4

In an XSLT, I want to convert an XML document to another one. The old document has some dates and times that aren't really easy to use. For example:

<foo date="20110310" time="002000" duration="001500"/>

Now I extracted all the information and was able to convert these to ISO 8601 dates:

<xsl:variable name="begin" select='concat($begin_date_year, "-", $begin_date_month, "-", $begin_date_day, "T", $begin_time_hour, ":", $begin_time_minutes, ":", $begin_time_seconds)'/>
--> $begin = 2011-03-10T00:20:00

And for the duration:

<xsl:variable name="duration" select='concat("PT", $dur_hour, ":", $dur_minutes, ":", $dur_seconds)'/>
--> $duration = PT00:15:00

How can I add the duration to the DateTime in order to find out the end (in a DateTime format)?

I already thought about adding the individual components, but this would involve a lot of fiddling around with moduli, for example if I added 15 minutes to 23:50 and then had to adjust the day accordingly, etc.

slhck
  • 36,575
  • 28
  • 148
  • 201

2 Answers2

6

Okay, now I found a function that wasn't listed in the function reference that I used before.

add-dayTimeDuration-to-dateTime(xs:dateTime, xs:dayTimeDuration)

This could also be written, for example, as:

xs:dateTime($begin) + xs:dayTimeDuration($duration)
slhck
  • 36,575
  • 28
  • 148
  • 201
  • 1
    That's XPath 2.0 and formaly it's an operator that _backs up the "`+`" operator on `xs:dateTime` and `xs:dayTimeDuration` values_ –  Mar 11 '11 at 15:33
0

Just for completeness, there is also an implementaton on http://www.exslt.org/date/functions/add/date.add.html

See a similar question at xslt - subtracting days

Community
  • 1
  • 1
wimh
  • 15,072
  • 6
  • 47
  • 98