Search code examples
c#.netdatetimeglobalization

Fill combobox with english months


I have to fill a combobox with month in English: January, February, etc. I did it next way:

private string[] AmericanMonths
{
  get
  {
    var arr = new string[12];
    var americanCulture = new CultureInfo("en-US");
    for (int i = 0; i < 12; i++)
    {
      arr[i] = new DateTime(2000, i + 1, 1).ToString("MMMM", americanCulture);
    }
    return arr;
  }
}

comboMonth.Items.AddRange(AmericanMonths);

How do you think, what way is better (perfomance)?

Is there a way to convert an integer representing month number to it's name?


Solution

  • The CultureInfo already contains the list of month names right in the framework. You could just write:

    var americanCulture = new CultureInfo("en-US");
    comboMonth.Items.AddRange(americanCulture.DateTimeFormat.MonthNames.Take(12)); //to remove last empty element