Add a Duration to DateTime in XSLT
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 t开发者_如何学运维he 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.
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)
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
精彩评论