Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


RE: Date comparison problem.

From: rdcpro@---------.------
To: NULL
Date: 2/2/2005 12:31:00 PM
The script you posted is rather odd, it's got elements from the old 1998 
working draft (MSXML 2-2.5) and a namespace for the XSLT spec (MSXML 3 and 
up).

If you're using MSXML or .NET, I'd probably handle the entire thing in a 
date function.  Personally, I'd use javascript, but you can use whatever 
language your processor supports.  You can even do this by creating an 
external class and adding it as an extension object, but the scripting 
approach is a little easier to understand IMO, at least at first.   Here's an 
example of how to do this with javascript extensions:

Using this XML:

<?xml version="1.0" standalone="yes"?>
<?xml-stylesheet type="text/xsl" href="demo.xslt"?>
<root>January 12, 1998</root>


With this XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:msxsl="urn:schemas-microsoft-com:xslt"
                xmlns:dates="urn:rdcpro-com:dates"
                >
    <xsl:output method="html" encoding="UTF-8"/>
    <msxsl:script language="JScript" implements-prefix="dates">
        <![CDATA[
            function parseDate(oNodeList) {
            var dateString = oNodeList[0].text;
            var t = Date.parse(dateString);
            return t;
            }
            function formatDate(t) {
            var oDate = new Date(t);
            return oDate.getFullYear() + "-" + (oDate.getMonth() + 1) + "-" 
+ oDate.getDate();
            }
            function dateAdd(t, sPeriod, iAdder) {
                var oDate = new Date(t);
                switch (sPeriod)
                {
                    case "day" :
                        oDate.setDate(oDate.getDate() + iAdder);
                        break;
                    case "month" :
                        oDate.setDate(oDate.getMonth() + iAdder);
                        break;
                    case "year" :
                        oDate.setDate(oDate.getYear() + iAdder);
                        break;
                }   
                return oDate.valueOf();
            }
        ]]>
    </msxsl:script>
    
    <xsl:template match="root">
        <table cellspacing="5px">
            <tr>
                <td>Input Date:</td>
                <td><xsl:value-of select="."/></td>
            </tr>
            <tr>
                <td>Formatted Date:</td>
                <td><xsl:value-of 
select="dates:formatDate(dates:parseDate(.))"/></td>
            </tr>
            <tr>
                <td>Add 2 days</td>
                <td><xsl:value-of 
select="dates:formatDate(dates:dateAdd(dates:parseDate(.), 'day', 2))"/></td>
            </tr>
            <tr>
                <td>Add 20 days</td>
                <td><xsl:value-of 
select="dates:formatDate(dates:dateAdd(dates:parseDate(.), 'day', 20))"/></td>
            </tr>
            <tr>
                <td>Subtract 2 months</td>
                <td><xsl:value-of 
select="dates:formatDate(dates:dateAdd(dates:parseDate(.), 'month', 
-2))"/></td>
            </tr>
            <tr>
                <td>Add 2 years</td>
                <td><xsl:value-of 
select="dates:formatDate(dates:dateAdd(dates:parseDate(.), 'year', 2))"/></td>
            </tr>
        </table>
    </xsl:template>
</xsl:stylesheet>


you get this result:


Input Date:            January 12, 1998 
Formatted Date:    1998-1-12 
Add 2 days            1998-1-14 
Add 20 days          1998-2-1 
Subtract 2 months 1997-12-29 
Add 2 years          1998-4-10 

Regards,
Mike Sharp


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent