none
How to get the current date in xslt, and minus this from another date? RRS feed

  • Question

  • Hi, I'm reading in a date/time from xml, and I'm wanting to subtract the current date from the other, and say how many hours/minutes the date is from now.

    I've found the XPath Extension functions, but there doesn't appear to be any function which can minus two date/times.

    How would I go about doing this?
    Sunday, June 15, 2008 12:06 AM

Answers

  • Unfortunately neither XPath 1.0 nor Xslt 1.0 offers any facilities to work with dates. You can however embed a script that will do the calculations for you, Here is an example:

     

    Code Snippet
      <ms:script language="javascript" implements-prefix="user">
        function datediff(date1, date2)
        {
        return Math.round(((new Date(date2)) - (new Date(date1))/1000/60);
        }
      </ms:script>

     

     

    Take a look at this MSDN article for more details: http://msdn.microsoft.com/en-us/library/ms256042(VS.85).aspx
     
    Pawel
    Sunday, June 15, 2008 1:43 AM
    Moderator
  • XSLT 2.0 can do that, here is a sample stylesheet

    Code Snippet

    <xsl:stylesheet
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      version="2.0"
      xmlns:xs="http://www.w3.org/2001/XMLSchema">
     
      <xsl:output method="text"/>
     
      <xsl:template match="root">
        <xsl:apply-templates select="date"/>
      </xsl:template>
     
      <xsl:template match="date">
        <xsl:text>The difference between </xsl:text>
        <xsl:value-of select="."/>
        <xsl:text> and </xsl:text>
        <xsl:value-of select="current-dateTime()"/>
        <xsl:text> is </xsl:text>
        <xsl:value-of select="current-dateTime() - xs:dateTime(.)"/>
      </xsl:template>

    </xsl:stylesheet>

     

     

    run against the following XML

    Code Snippet

    <root>
      <date>2008-06-15T12:00:00Z</date>
    </root>

     

     

    it outputs

    Code Snippet

    The difference between 2008-06-15T12:00:00Z and 2008-06-15T16:37:55.64+02:00 is
    PT2H37M55.64S

     

     

    There are currently three XSLT 2.0 processors, Saxon from http://saxon.sourceforge.net/, Gestalt from http://gestalt.sourceforge.net/, and Altova XML tools http://www.altova.com/altovaxml.html

    Saxon and Altova have a .NET API so you can use them from your .NET application.

    Sunday, June 15, 2008 2:39 PM

All replies

  • Unfortunately neither XPath 1.0 nor Xslt 1.0 offers any facilities to work with dates. You can however embed a script that will do the calculations for you, Here is an example:

     

    Code Snippet
      <ms:script language="javascript" implements-prefix="user">
        function datediff(date1, date2)
        {
        return Math.round(((new Date(date2)) - (new Date(date1))/1000/60);
        }
      </ms:script>

     

     

    Take a look at this MSDN article for more details: http://msdn.microsoft.com/en-us/library/ms256042(VS.85).aspx
     
    Pawel
    Sunday, June 15, 2008 1:43 AM
    Moderator
  • Thanks,

    I managed to solve my problem by changing the xml in c# before I applied xslt.

    It's good to know that you can embed scrip into xslt.
    Sunday, June 15, 2008 3:14 AM
  • XSLT 2.0 can do that, here is a sample stylesheet

    Code Snippet

    <xsl:stylesheet
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      version="2.0"
      xmlns:xs="http://www.w3.org/2001/XMLSchema">
     
      <xsl:output method="text"/>
     
      <xsl:template match="root">
        <xsl:apply-templates select="date"/>
      </xsl:template>
     
      <xsl:template match="date">
        <xsl:text>The difference between </xsl:text>
        <xsl:value-of select="."/>
        <xsl:text> and </xsl:text>
        <xsl:value-of select="current-dateTime()"/>
        <xsl:text> is </xsl:text>
        <xsl:value-of select="current-dateTime() - xs:dateTime(.)"/>
      </xsl:template>

    </xsl:stylesheet>

     

     

    run against the following XML

    Code Snippet

    <root>
      <date>2008-06-15T12:00:00Z</date>
    </root>

     

     

    it outputs

    Code Snippet

    The difference between 2008-06-15T12:00:00Z and 2008-06-15T16:37:55.64+02:00 is
    PT2H37M55.64S

     

     

    There are currently three XSLT 2.0 processors, Saxon from http://saxon.sourceforge.net/, Gestalt from http://gestalt.sourceforge.net/, and Altova XML tools http://www.altova.com/altovaxml.html

    Saxon and Altova have a .NET API so you can use them from your .NET application.

    Sunday, June 15, 2008 2:39 PM
  • xsltsl offers a library to perform dates calculations in pure XSLT 1.0.

    See the Date/Time Processing module (example below).

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:dt="http://xsltsl.org/date-time"
     xmlns:str="http://xsltsl.org/string"
     extension-element-prefixes="dt str"
    >
     <xsl:import href="http://xsltsl.sourceforge.net/modules/string.xsl"/>
     <xsl:import href="http://xsltsl.sourceforge.net/modules/date-time.xsl"/>
    
    ...
    
     <xsl:variable name="first-julian">
      <xsl:call-template name="dt:calculate-julian-day">
       <xsl:with-param name="year" select="$year"/>
       <xsl:with-param name="month" select="$month"/>
       <xsl:with-param name="day" select="$first"/>
      </xsl:call-template>
     </xsl:variable>
    
     <xsl:call-template name="dt:format-julian-day">
      <xsl:with-param name="julian-day" select="$first-julian+$nb-days -1"/>
      <xsl:with-param name="format" select="'%Y-%m-%d'"/>
     </xsl:call-template>
    

    … Or you can use the various implementations made available by EXSLT. See EXSLT - Dates and Times.


    Wednesday, May 25, 2011 3:28 AM
  • I had to get previous date at work with only XSLT 1.0 available to me. As result, I've come up with a transformation, that can do just that. You can check it on Github: https://github.com/Simbiat/xslt1_previous_day
    Saturday, August 31, 2019 7:11 AM