Altova Mailing List Archives>Archive Index >microsoft.public.xsl Archive Home >Recent entries >Thread Prev - Date comparison problem. [Thread Next] RE: Date comparison problem.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 | ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
