The other day I was working on a project where it was important to get the full name of the month from a specified date. So off I went to start writing an array that contained the full name of all the months. In the back of my mind, however, my coder’s conscience was letting me know this solution was a path to problems. I started thinking of the maintenance nightmares that would ensue when issues such as abbreviated month names and multi-lingual support would start to creep in (and turned out to be a requirement for this particular client). The last thing I want to do is try to keep up with lists of months and abbreviations in French or German or Hindi. So I went in search of a better solution and my travels revealed the .NET framework already supported this functionality.
The secret can be found in the CultureInfo class. Not only did this eliminate the need for multiple lists for names and abbreviations, it also has multi-lingual support. Score!
The method looks like this:
And this returns January. I didn’t want to have to type this out every time a month name was needed so it was time for the help of an extension method:
Now I can get the full name of a month from any culture. But wait, there’s more…