Contents tagged with globalization

  • 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.

    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#:

    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:


    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();


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

    DateTimeFormatInfo info = new DateTimeFormatInfo();


    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.