Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old August 13th, 2010, 08:08 AM
Friend of Wrox
Points: 1,243, Level: 13
Points: 1,243, Level: 13 Points: 1,243, Level: 13 Points: 1,243, Level: 13
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2003
Location: , , United Kingdom.
Posts: 290
Thanks: 24
Thanked 0 Times in 0 Posts
Default How to add 12 moths to a date

Hi,

1 - Please could someone shed some light on how to add 12 months (1 year) to a start date?

In the xml below I have an AnchorDate element that contains Year and Month elements.

They form the start date like this - 201008 (YYYYMM)

Based on these values I need to get the end date that will be 1 year later.

2 - Also, is there a better way to concat a 0 (zero) in front of the month if the month is less than 10 or my approach is ok?

Cheers

P

<Schedule>
<AnchorDate>
<Month value="8"></Month>
<Year value="2010"></Year>
</AnchorDate>
</Schedule>

<xsl:variable name ="monthDate">
<xsl:if test ="Schedule/AnchorDate/Month/@value &lt; 10">
<xsl:value-of select ="concat('0', Schedule/AnchorDate/Year/@value,$monthDate)"/>
</xsl:variable>



<xsl:variable name ="endDate">
<xsl:value-of select ="concat(
Reply With Quote
  #2 (permalink)  
Old August 13th, 2010, 08:09 AM
Friend of Wrox
Points: 1,243, Level: 13
Points: 1,243, Level: 13 Points: 1,243, Level: 13 Points: 1,243, Level: 13
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2003
Location: , , United Kingdom.
Posts: 290
Thanks: 24
Thanked 0 Times in 0 Posts
Default

Hi,

For some reason my text is not formatting well. I will post the code below again.


<Schedule>
<
AnchorDate>
<
Monthvalue="8"></Month>
<
Yearvalue="2010"></Year>
</
AnchorDate>
</
Schedule>

<
xsl:variablename ="monthDate">
<
xsl:iftest ="Schedule/AnchorDate/Month/@value &lt; 10">
<
xsl:value-ofselect ="concat('0', Schedule/AnchorDate/Month/@value)"/>
</
xsl:if>
</
xsl:variable>

<
xsl:variablename ="startDate">
<
xsl:value-ofselect ="concat(Schedule/AnchorDate/Year/@value,$monthDate)"/>
</
xsl:variable>

<
xsl:variablename ="endDate">
<
xsl:value-ofselect ="concat(Schedule/AnchorDate/Year/@value,$monthDate)"/>
</
xsl:variable>

Reply With Quote
  #3 (permalink)  
Old August 13th, 2010, 08:21 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Why not just do "Schedule/AnchorDate/Year/@value+1" ?
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
Reply With Quote
  #4 (permalink)  
Old August 13th, 2010, 08:22 AM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

The answers to both questions depend on whether you are using XSLT 1.0 or 2.0.

However, since your year and month are in separate elements, I would have thought you could simply add 1 to the year.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
Reply With Quote
  #5 (permalink)  
Old August 13th, 2010, 08:30 AM
Friend of Wrox
Points: 1,243, Level: 13
Points: 1,243, Level: 13 Points: 1,243, Level: 13 Points: 1,243, Level: 13
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2003
Location: , , United Kingdom.
Posts: 290
Thanks: 24
Thanked 0 Times in 0 Posts
Default

Hi Michael and Sam,

Thanks for your reply.

Sorry, I forgot to mention that I am using XSLT 1 and MSXML.

1 - But what about the month?

If my start date is 201008 then my end date has to be 201107

2 - Also what happans when my start date is 201001?

In this case I would not have to add 1 to year and the end date would be:

201012

Cheers

P
Reply With Quote
  #6 (permalink)  
Old August 13th, 2010, 08:44 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

That isn't adding 12 months - that's adding 11.

Code:
<xsl:variable name="month" select="Schedule/AnchorDate/Month/@value"/>
<xsl:variable name="year" select="Schedule/AnchorDate/Year/@value"/>

<xsl:variable name="newYear">
<xsl:choose><xsl:when test="$month = 1"><xsl:value-of select="$year"/></xsl:when>
<xsl:otherwise><xsl:value-of select="$year+1"/></xsl:otherwise>
</xsl:choose>
</xsl:variable>


<xsl:variable name="newMonth">
<xsl:choose>
<xsl:when test="$month = 1"><xsl:value-of select="'12'"/></xsl:when>
<xsl:when test="$month &gt; 10"><xsl:value-of select="$month-1"/></xsl:when>
<xsl:otherwise><xsl:value-of select="concat('0',$month-1)"/></xsl:otherwise>
</xsl:choose>
</xsl:variable>

<xsl:variable name="endDate">
<xsl:value-of select="$newYear"/><xsl:value-of select="$newMonth"/>
</xsl:variable>
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
Reply With Quote
The Following User Says Thank You to samjudson For This Useful Post:
pallone (August 13th, 2010)
  #7 (permalink)  
Old August 13th, 2010, 08:47 AM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Well, you said you wanted to add 12 months. Adding 11 months is more difficult. Take a look at the templates in the EXSLT library for date and time arithmetic, which handle a lot of this for XSLT 1.0. (www.exslt.org)
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
Reply With Quote
The Following User Says Thank You to mhkay For This Useful Post:
pallone (August 13th, 2010)
  #8 (permalink)  
Old August 13th, 2010, 08:50 AM
Friend of Wrox
Points: 1,243, Level: 13
Points: 1,243, Level: 13 Points: 1,243, Level: 13 Points: 1,243, Level: 13
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2003
Location: , , United Kingdom.
Posts: 290
Thanks: 24
Thanked 0 Times in 0 Posts
Smile

Hi Sam,

Thanks for the source code. I will try that straight away after lunch.

I really appreciate your help
Reply With Quote
  #9 (permalink)  
Old August 13th, 2010, 08:51 AM
Friend of Wrox
Points: 1,243, Level: 13
Points: 1,243, Level: 13 Points: 1,243, Level: 13 Points: 1,243, Level: 13
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2003
Location: , , United Kingdom.
Posts: 290
Thanks: 24
Thanked 0 Times in 0 Posts
Default

Hi Michael,

Does the EXSL library works for both IE 6 + and Firefox 3 + ?

Cheers

P
Reply With Quote
  #10 (permalink)  
Old August 13th, 2010, 09:32 AM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

>Does the EXSL library works for both IE 6 + and Firefox 3 + ?

Some EXSLT functions are implemented as native code for particular XSLT processors, and some are implemented as generic XSLT 1.0 source code templates that can be incorporated into any stylesheet using xsl:import or xsl:include. You'll have to check the details yourself.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
Reply With Quote
The Following User Says Thank You to mhkay For This Useful Post:
pallone (August 13th, 2010)
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to add Weeks to a date? chobo2 ASP.NET 3.5 Basics 2 March 20th, 2009 07:01 PM
Add Date to File Using Cmd ecom Other Programming Languages 1 November 13th, 2007 05:16 PM
want to convert date from 01/12/07 to 12-Jan-2007 hurperl Perl 1 April 12th, 2007 05:47 AM
12/30/1899 ASP Date from SQL chris42480 Classic ASP Databases 2 March 8th, 2004 02:23 PM



All times are GMT -4. The time now is 06:27 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.