views:

205

answers:

2

I am trying to get the number of months between two dates in my xslt file like so:

<xsl:variable name="now" select="umbraco.library:CurrentDate()"/>
<xsl:value-of select="umbraco.library:DateDiff('2010-12-01', $now, 'm')" />

Unfortunately, this seems to give me the number of minutes between the two dates rather than the number of months. I can't find anywhere the string to enter as the third parameter for months. Is there are reference for this somewhere? Or how do I find the number of months?

+3  A: 

The DateDiff method does not support months.

It only supports years, minutes or seconds.

To calculate the difference in months you'll need to do some maths juggling or use an inline c# method or write your own XSLT extension method.

More info on inline c#

There are many tutorials for creating XSLT extension methods, including several video tutorials on he main umbraco site.

More info on XSLT exensions

Umbraco videos on XSLT extensions

Tim Saunders
A: 

I ended up writing my own function like Tim suggested:

public static int MonthDiff(string date1, string date2)
{
    DateTime dt1 = DateTime.Parse(date1);
    DateTime dt2 = DateTime.Parse(date2);

    return ((dt2.Year - dt1.Year) * 12) + (dt2.Month - dt1.Month);
}

I'm sure it could be better generalized, but this was good enough for me.

Soldarnal