.NET - Retrieving the Month Name

Here’s another amusing method I recently found in some production code which comes straight from the “can’t be bothered to learn the framework (or how to Google)” style of programming:

/// <summary>
/// Converts a month number into the name
/// </summary>
/// <param name="monthNumber"></param>
private string GetMonthName(int monthNumber)
{
    return String.Format("{0:MMMM}", new DateTime(1969, monthNumber, 1)); 
}

I wonder what the significance of 1969 is? Year of the author's birth, perhaps?!

Aside from being hacky, there is a performance implication to unnecessarily creating new DateTime objects and performing string formatting in this way. In my tests, I’ve found that this method is around 18 times slower than simply calling System.Globalization.DateTimeFormatInfo.CurrentInfo.GetMonthName(monthNumber).

#1  avatar Jeremy Pearson on 08 Jun 2009 at 13:36

1969 seems like an odd number (no pun intended). In the Linux/Unix World, time began on 1 Jan 1970. These systems count seconds elapsed ever since. The counter is declared as a signed 32-bit integer, which is fine .. we won't be using Linux/Unix after 2106, will we?

Leave a Comment