Topic
• 7 replies
• Latest Post - ‏2013-12-27T22:44:57Z by HermannSW
RiyazShah
58 Posts

# Pinned topic How to calculate the Duration between two dates in xslt

‏2013-11-21T19:34:40Z |

Hello Developers

I have a scenario that to calculate the duration between current date and before date.

sample before date is given as 2013-07-24T09:12:47.820-05:00.

could anybody help me how to calculate the difference in xslt.

• HermannSW
7643 Posts

#### Re: How to calculate the Duration between two dates in xslt

‏2013-11-21T22:42:37Z

You get current date-time by date:date-time() extension function.

date:difference() extension function calculates the duration you want.

• dar99
7 Posts

#### Re: How to calculate the Duration between two dates in xslt

‏2013-11-22T16:41:38Z
• HermannSW
• ‏2013-11-21T22:42:37Z

You get current date-time by date:date-time() extension function.

date:difference() extension function calculates the duration you want.

2013-11-21T14:10:40.5Z

Why there is Z in the DataPower time, What does Z means?

• kenhygh
2474 Posts

#### Re: How to calculate the Duration between two dates in xslt

‏2013-11-22T17:25:46Z
• dar99
• ‏2013-11-22T16:41:38Z

2013-11-21T14:10:40.5Z

Why there is Z in the DataPower time, What does Z means?

• samuels
64 Posts

#### Re: How to calculate the Duration between two dates in xslt

‏2013-11-22T19:15:00Z
• HermannSW
• ‏2013-11-21T22:42:37Z

You get current date-time by date:date-time() extension function.

date:difference() extension function calculates the duration you want.

hi hermann,

i have the same requirement ,i tried i like this but its not  working

<xsl:variable name="current_date" select="date:date()"/>
<xsl:variable name="old_date" select="2013-11-19"/>
<xsl:variable name="datediff" select="date:difference('\$current_date','\$old_date')">
</xsl:variable>

• HermannSW
7643 Posts

#### Re: How to calculate the Duration between two dates in xslt

‏2013-11-22T19:50:18Z
• samuels
• ‏2013-11-22T19:15:00Z

hi hermann,

i have the same requirement ,i tried i like this but its not  working

<xsl:variable name="current_date" select="date:date()"/>
<xsl:variable name="old_date" select="2013-11-19"/>
<xsl:variable name="datediff" select="date:difference('\$current_date','\$old_date')">
</xsl:variable>

Hi,

you had missing apostrophes in \$old_date definition, and superfluous apostrophes in difference funtion call.

\$ coproc2 ddiff.xsl dummy.xml http://dp3-l3:2223 ; echo
-P3D
\$
\$ cat ddiff.xsl
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:date="http://exslt.org/dates-and-times"
>
<xsl:output method="text"/>

<xsl:variable name="current_date" select="date:date()"/>
<xsl:variable name="old_date" select=" '2013-11-19' "/>
<xsl:variable name="datediff"
select="date:difference(\$current_date,\$old_date)"/>

<xsl:template match="/">
<xsl:value-of select="\$datediff"/>
</xsl:template>
</xsl:stylesheet>
\$

• JEK1
6 Posts

#### Re: How to calculate the Duration between two dates in xslt

‏2013-12-27T18:37:56Z
• HermannSW
• ‏2013-11-22T19:50:18Z

Hi,

you had missing apostrophes in \$old_date definition, and superfluous apostrophes in difference funtion call.

\$ coproc2 ddiff.xsl dummy.xml http://dp3-l3:2223 ; echo
-P3D
\$
\$ cat ddiff.xsl
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:date="http://exslt.org/dates-and-times"
>
<xsl:output method="text"/>

<xsl:variable name="current_date" select="date:date()"/>
<xsl:variable name="old_date" select=" '2013-11-19' "/>
<xsl:variable name="datediff"
select="date:difference(\$current_date,\$old_date)"/>

<xsl:template match="/">
<xsl:value-of select="\$datediff"/>
</xsl:template>
</xsl:stylesheet>
\$

I am trying to use your example to calculate the date and time difference between to 2 variables that I have created based on timestamp created and timestamp expires.  I don't seem to be able to see what the value of your datediff variable is on the datapower.  Is there somewhere this will be stored?  I think I saw something in xsl test tool that indicated the value from the date:difference function to be in this format - PT5M...(I have set the value between my created and expires to be 5 minutes).

• HermannSW
7643 Posts

#### Re: How to calculate the Duration between two dates in xslt

‏2013-12-27T22:44:57Z
• JEK1
• ‏2013-12-27T18:37:56Z

I am trying to use your example to calculate the date and time difference between to 2 variables that I have created based on timestamp created and timestamp expires.  I don't seem to be able to see what the value of your datediff variable is on the datapower.  Is there somewhere this will be stored?  I think I saw something in xsl test tool that indicated the value from the date:difference function to be in this format - PT5M...(I have set the value between my created and expires to be 5 minutes).

The spec explains why "-P3D" is response above, and "PT1H44M57S" below:

The date/time with the most specific format (i.e. the least truncation) is converted into the same format as the date with the most specific format (i.e. the most truncation).

\$ coproc2 dtdiff.xsl dummy.xml http://dp3-l3:2223; echo
PT1H44M57S
\$
\$ cat dtdiff.xsl
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:date="http://exslt.org/dates-and-times"
>
<xsl:output method="text"/>

<xsl:variable name="current_date-time" select="date:date-time()"/>
<xsl:variable name="old_date-time" select=" '2013-12-27T22:00:00+01:00' "/>
<xsl:variable name="datetimediff"
select="date:difference(\$old_date-time,\$current_date-time)"/>

<xsl:template match="/">
<xsl:value-of select="\$datetimediff"/>
</xsl:template>
</xsl:stylesheet>
\$