Simple Year Pivoting Function

Year after year you know you have a product that will be updated at a[round] a certain month. It happens a lot with my business domain. Rather than changing lots of text, this should work nicely for my needs. (I didn't use any timezone or culture considerations)

the pivot month is the int that decides when we flip to next year.

/// <summary>
/// Figures out if should use this year 
///or next based on a defined pivot month.
/// </summary>
/// <returns>4 digit year</returns>
public int YearPivot(DateTime dt, int pivotMonth)
     return (dt.Month <= pivotMonth) ? dt.Year : dt.AddYears(1).Year;

couple tests showing it in action...

DateTime dt = new DateTime(2009, 10, 1);
//returns 2009
Assert.AreEqual(2009, m.YearPivot(dt, 10));
//returns 2010
Assert.AreEqual(2010, m.YearPivot(dt, 5));

brian's picture

no problem!

Submitted by brian on Thu, 10/29/2009 - 4:02am.
thanks for pivot

great job bos,thank a wish for you.

Submitted by naniek (not verified) on Wed, 10/28/2009 - 1:43am.

About Brian

profile for bluevoodoo1 on Stack Exchange, a network of free, community-driven Q&A sites

Brian Canzanella brings you nifty tips and tricks for most things .NET. read more...

Readers / Stuff