How to list all month names, e.g. for a combo?

76,709

Solution 1

You can use the DateTimeFormatInfo to get that information:

// Will return January
string name = DateTimeFormatInfo.CurrentInfo.GetMonthName(1);

or to get all names:

string[] names = DateTimeFormatInfo.CurrentInfo.MonthNames;

You can also instantiate a new DateTimeFormatInfo based on a CultureInfo with DateTimeFormatInfo.GetInstance or you can use the current culture's CultureInfo.DateTimeFormat property.

var dateFormatInfo = CultureInfo.GetCultureInfo("en-GB").DateTimeFormat;

Keep in mind that calendars in .Net support up to 13 months, thus you will get an extra empty string at the end for calendars with only 12 months (such as those found in en-US or fr for example).

Solution 2

This method will allow you to apply a list of key value pairs of months to their int counterparts. We generate it with a single line using Enumerable Ranges and LINQ. Hooray, LINQ code-golfing!

var months = Enumerable.Range(1, 12).Select(i => new { I = i, M = DateTimeFormatInfo.CurrentInfo.GetMonthName(i) });

To apply it to an ASP dropdown list:

// <asp:DropDownList runat="server" ID="ddlMonths" />
ddlMonths.DataSource = months;
ddlMonths.DataTextField = "M";
ddlMonths.DataValueField = "I";
ddlMonths.DataBind();

Solution 3

You can use the following to return an array of string containing the month names

System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.MonthNames

Solution 4

They're defined as an array in the Globalization namespaces.

using System.Globalization;

for (int i = 0; i < 12; i++) {
   Console.WriteLine(CultureInfo.CurrentUICulture.DateTimeFormat.MonthNames[i]);
}

Solution 5

public IEnumerable<SelectListItem> Months
{
  get
  {
    return Enumerable.Range(1, 12).Select(x => new SelectListItem
    {
      Value = x.ToString(),
      Text = DateTimeFormatInfo.CurrentInfo.GetMonthName(x)
    });
  }
}
Share:
76,709

Related videos on Youtube

ProfK
Author by

ProfK

I am a software developer in Johannesburg, South Africa. I specialise in C# and ASP.NET, with SQL Server. I have, in some way or another, been involved in software development for about eighteen years, but always learning something new. At the moment that is WPF and MVVM.

Updated on July 05, 2022

Comments

  • ProfK
    ProfK almost 2 years

    At the moment I'm creating a DateTime for each month and formatting it to only include the month.
    Is there another or any better way to do this?

  • Greg
    Greg over 15 years
    For some reason the code I grabbed was using the Invariant name for the ListItem.Value property. Not sure why, but you might want to use an integer for that instead.
  • Sarawut Positwinyu
    Sarawut Positwinyu over 12 years
    I use CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName
  • The Muffin Man
    The Muffin Man over 11 years
    Your second example won't compile. You need to create a new instance of DateTimeFormatInfo or call its GetInstance() method from the static class .e.g. new DateTimeFormatInfo().MonthNames; or DateTimeFormatInfo.GetInstance().MonthNames
  • Sam
    Sam almost 11 years
    I've corrected the issues noted by the comments (in the future you can just edit the post).
  • John
    John over 9 years
    Like the use of Enumerable.Range , I'm going to use this with a List of MVC SelectListItems
  • RoLYroLLs
    RoLYroLLs about 6 years
    up'd for that Where clause
  • CompuChip
    CompuChip over 5 years
    Note: I had to use the answer with the Enumerable.Range because DateTimeFormatInfo.MonthNames returns an array with 13 elements so if you .Select on that you get an empty item at the end.
  • ProfK
    ProfK over 5 years
    Very nice LINQ.
  • Andrew Truckle
    Andrew Truckle over 3 years
    Thanks! TakeWhile was just what I needed. :)