Date Handling Tip: Get the Month Name

There are a lot of great date handling methods attached to the DateTime class in .NET. However, if you wish to get information such as the current month name, you will not find it in this class. The reason is because .NET needs to take into account local culture and languages. Thus, methods that return something that is specific to a language or a culture are placed under the System.Globalization namespace. As an example, below is the code you would need to write to return the month name as a string in any language.

VB.NET
Imports System.Globalization

Public Shared Function GetMonthName(ByVal dateValue As Date) As String
  Dim info As New DateTimeFormatInfo
  Dim names() As String

  names = info.MonthNames

  Return names(dateValue.Month - 1)
End Function

Here is the same code in C#:

C#
using System.Globalization;

public static string GetMonthName(DateTime dateValue)
{
  DateTimeFormatInfo info = new DateTimeFormatInfo();
  string[] names;

  names = info.MonthNames;

  return names[dateValue.Month - 1];
}

In the code above, you create an instance of the DateTimeFormatInfo class. This class is located in the System.Globalization namespace. It is this class that has properties for the month names, day names, abbreviated month names, the month separator character and other characteristics of dates that can change with local culture.

You call the GetMonthName method presented above and pass in any valid date/time value and the month name for that date will be returned. For example, if you were to wrap the above method into a class called MyDates, then you would write code as shown below:

MessageBox.Show(MyDates.GetMonthName(DateTime.Now));

Now, of course, you do not need to wrap up the calls to the DateTimeFormatInfo as I did here. You can just use the properties and methods of the DateTimeFormatInfo class directly. I presented it as I did above for clarity. You can always just call any of the various properties and methods directly. For example, there is a GetMonthName() method that you can call and just pass in the month number.

DateTimeFormatInfo info = new DateTimeFormatInfo();

MessageBox.Show(info.GetMonthName(DateTime.Now.Month));

You can also get any of the day names of the weeks by using the GetDayName() method.

DateTimeFormatInfo info = new DateTimeFormatInfo();

MessageBox.Show(info.GetDayName(0));

There is much more to the DateTimeFormatInfo class than I presented here, so take some time and check out all the various methods and properties of this powerful class.

Past Blog Content

Blog Archive

3 Comments

  • If you add the word "this" to your method you create an extension method:

    public static string GetMonthName(this DateTime dateValue)

    This allows you to do code like:

    DateTime.Now.GetMonthName()

  • That code will return the name for the invariant culture. If you want to return the name for the current culture, replace "new DateTimeFormatInfo" with "DateTimeFormatInfo.CurrentInfo".

  • string DateTime.ToString( string format ) will provide the same results.
    e.g.
    string monthName = DateTime.Today.ToString( "MMMM" );
    The same can be used for formatting a date to just about any need.
    e.g.
    string monthAndYear = DateTime.Today.ToString( "MMMM yyyy")
    ToString uses the thread current culture.  There is an overload to use a specific culture as well.
    So, assuming you are using 3.5, you could write an extension method like:
    public static string MonthName( this DateTime dateValue )
    {
     return dateValue.ToString( "MMMM" );
    }
    which could be used like:
    MessageBox.Show( DateTime.Now.MonthName() );
    Just an alternative to your approach.  Nice post.  I'll be taking a look at the DateTimeFormatInfo more closely.

Comments have been disabled for this content.