Home > Mobile >  How to calculate year old date range from the current date(EDT) in xslt 1.0
How to calculate year old date range from the current date(EDT) in xslt 1.0

Time:09-23

Could some one please suggest me out to calculate year old date range from the current date time which is in EDT format using XSLT 1.0.

I know using xslt 2.0 there are function like 'yearMonthDuration' but need to perform this in xslt 1.0.

I have this ''' 2021-09-21 11:14:20 EDT '''

Can I extract year column and add 365 to this will that work? Please help in this regard.

CodePudding user response:

EXSLT.Date has some support e.g.

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:date="http://exslt.org/dates-and-times"
  exclude-result-prefixes="xs date">

  <xsl:template match="date">
    <xsl:copy>
      <xsl:value-of select="concat(., ' - ', date:add(., 'P1Y'))"/>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

gives <date>2021-09-22 - 2022-09-22</date> for an input of e.g. <date>2021-09-22</date>.

So check whether you use processor supporting that or can use an extension to your XSLT processor, for .NET XslCompiledTransform there is one with https://www.nuget.org/packages/Mvp.Xml.NetStandard/.

CodePudding user response:

Can I extract year column and add 365 to this will that work?

If all you want to do is add 1 year to a given date, you could start by extracting the year component and simply adding 1 to it, leaving the month and day components as they are. However, this will produce an invalid date if the given date is February 29 in a leap year. Still, it's rather simple to correct for that - say something like:

    <xsl:value-of select="substring($given-date, 1, 4)   1"/>
    <xsl:variable name="md" select="substring($given-date, 5, 10)" />
    <xsl:choose>
        <xsl:when test="$md='-02-29'">-02-28</xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$md"/>
        </xsl:otherwise>
    </xsl:choose>
  • Related