Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 7th, 2007, 02:54 PM
Authorized User
 
Join Date: Jun 2007
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Default date arithmetic

Does XSLT support a way to modify gregorian dates? For example, if given a value using a simple date format (mm/dd/yyyy 00:00:00), I want to add 3 months to that date and return the value in the same simple date format.

 
Old June 7th, 2007, 04:32 PM
Authorized User
 
Join Date: Jun 2007
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Default

In case anyone is interested, date conversion answers maybe found at http://www.dpawson.co.uk/xsl/rev2/dates.html

 
Old June 7th, 2007, 05:23 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

XSLT 2.0 has lots of support for date manipulation. But it's a good idea to use the ISO standard date format YYYY-MM-DDTHH:MM:SS (if you don't, you'll have to convert into that format before you do anything else).

There's no date/time support in XSLT 1.0 itself, but there's a date/time library at www.exslt.org which is supported by a number of implementations.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old June 8th, 2007, 09:51 AM
Authorized User
 
Join Date: Jun 2007
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks. I have one lingering question and that is how do I modify the year using xs:dayTimeDuration? Is that possible or do I have to use another function?

<xsl:template match="/">

<xsl:variable name="date" select="date"/>
<xsl:variable name="current_date" select="bf:to-iso-date($date)"/>
<xsl:variable name="date_increase" select="string('PT5Y')"/>
<xsl:variable name="modified_date" select="string(xs:dateTime($current_date) + xs:dayTimeDuration($date_increase))"/>
<xsl:value-of select="bf:to-yyyymmdd-date($modified_date)"/>
</xsl:template>

<xsl:function name="bf:to-iso-date" as="xs:dateTime">
<xsl:param name="in" as="xs:string"/>
<xsl:sequence select="xs:dateTime(replace($in, '(\d{2})/(\d{2})/(\d{4}) ([\d:]{8})','$3-$1-$2T$4'))"/>
</xsl:function>

<xsl:function name="bf:to-yyyymmdd-date" as="xs:string">
<xsl:param name="in" as="xs:string"/>
<xsl:sequence select="xs:string(replace($in, '(\d{4})-(\d{2})-(\d{2})T([\d:]{8})','$2/$3/$1 $4'))"/>
</xsl:function>


 
Old June 8th, 2007, 09:58 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You can do this but you need a yearMonthDuration not a dayTimeDuration (and there's no "T" in the literal):

<xsl:template match="/">

<xsl:variable name="date" select="date"/>
<xsl:variable name="current_date" select="bf:to-iso-date($date)"/>
<xsl:variable name="date_increase" select="'P5Y'"/>
<xsl:variable name="modified_date" select="string(xs:dateTime($current_date) + xs:yearMonthDuration($date_increase))"/>
<xsl:value-of select="bf:to-yyyymmdd-date($modified_date)"/>
</xsl:template>


Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP date arithmetic takabyte PHP How-To 4 February 12th, 2009 02:54 PM
Arithmetic overflow error gregalb Reporting Services 1 June 24th, 2008 02:42 PM
Basic- Making a template call / Date Arithmetic presack XSLT 2 August 3rd, 2007 01:33 PM
arithmetic with ASP VBScript dcarroll Classic ASP Basics 2 October 7th, 2005 09:14 AM
arithmetic overflow problem flyfish SQL Server 2000 4 July 12th, 2005 04:37 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.