Subject: RE: [xsl] Calculating months between two dates From: "Michael Kay" <mike@xxxxxxxxxxxx> Date: Fri, 1 Jun 2007 20:16:28 +0100 |
There was a function to do this in earlier XSLT 2.0 drafts - subtract-dates-yielding-yearMonthDuration or some such - but it was removed because of difficulties in defining exactly what it should do. One possible algorithm, which you can easily implement as a function, is (year-from-date($d1) - year-from-date($d2))*12 + (month-from-date($d1) - month-from-date($d2)) - if (days-from-date($d1) ge days-from-date($d2)) then 0 else 1 That works reasonably for $d2 < $d1. To handle $d1 < $d2 one approach is to use the same formula as above, another is to return -($d2 - $d1). For 2007-05-31 - 2007-06-01 one approach gives you -1, the other gives you 0. Another algorithm is to ignore the day entirely, and just use the year and month components. Or you can divide the day by the number of days in that month... Michael Kay http://www.saxonica.com/ > -----Original Message----- > From: Ryan Puddephatt [mailto:rpuddephatt@xxxxxxxxxxxx] > Sent: 01 June 2007 19:52 > To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx > Subject: [xsl] Calculating months between two dates > > Hi, > I want to know if it is possible to calculate the number > of months between two dates. For example > xs:duration(xs:date('2009-09-23') - > current-date()) > > From this I get a result in days, is there a way to convert > this or change its result? > > Thanks > > Ryan > -- > > Ryan Puddephatt > Software Engineer > > Teleflex Group - IT UK > 1 Michaelson Square > Livingston > West Lothian > Scotland > EH54 7DP > > e> rpuddephatt@xxxxxxxxxxxx <mailto:rpuddephatt@xxxxxxxxxxxx> > t> +44(0)1506 407 110 > f> +44(0)1506 407 108 > w> www.teleflex.com <http://www.teleflex.com> > > "Measuring programming progress by lines of code is like > measuring aircraft building progress by weight." - /Bill > Gates/ "If you lie to the compiler, it will get its revenge." > - /Henry Spencer/ "It's hard enough to find an error in your > code when you're looking for it; it's even harder when you've > assumed your code is error-free." - /Steve McConnell/ "If > builders built buildings the way programmers wrote programs, > then the first woodpecker that came along would destroy > civilization." - /Gerald Weinberg/
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
[xsl] Calculating months between tw, Ryan Puddephatt | Thread | Re: [xsl] Calculating months betwee, Eric Larson |
[xsl] Calculating months between tw, Ryan Puddephatt | Date | Re: [xsl] Calculating months betwee, Eric Larson |
Month |