Abhi
Abhi

Reputation: 303

How to subtract 1 day (calculate day before) in XSLT 2.0?

Could someone please help calculate one day ago from a given date of type xsd:dateTime using XSLT 2.0?

Sample Input:

<?xml version="1.0" encoding="UTF-8"?>
<objects>
<LastRunTime__c>
<Last_Run_Time__c>2015-01-29T19:55:15.846Z</Last_Run_Time__c>
</LastRunTime__c>
</objects>

Sample Output:

<?xml version="1.0" encoding="UTF-8"?>
<objects>
<LastRunTime__c>
<Last_Run_Time__c>2015-01-28T19:55:15.846Z</Last_Run_Time__c>
</LastRunTime__c>
</objects>

I was trying a few things but was not able to get what I want:

<xsl:template match="/">
<xsl:variable name="vToday" select="/*:objects/*:LastRunTime__c/*:Last_Run_Time__c"/>
Today is: <xsl:sequence select="$vToday"/>
1 day ago it was: <xsl:sequence select="$vToday -1*xs:dayTimeDuration('P1D')"/>
</xsl:template>
</xsl:stylesheet>

Upvotes: 2

Views: 4891

Answers (1)

kjhughes
kjhughes

Reputation: 111621

Given your input XML, this XSLT transformation:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsl:output method="text"/>
  <xsl:template match="/">
    <xsl:variable name="vToday" select="/objects/LastRunTime__c/Last_Run_Time__c" 
                  as="xsd:dateTime"/>
    Today is: <xsl:value-of select="$vToday"/>
    1 day ago it was: <xsl:value-of select="$vToday - xsd:dayTimeDuration('P1D')"/>
  </xsl:template>
</xsl:stylesheet>

Will produce the desired output:

    Today is: 2015-01-29T19:55:15.846Z
    1 day ago it was: 2015-01-28T19:55:15.846Z

Upvotes: 4

Related Questions